Conditional compiling in Swift - ios

I want to use flags to control the compiler in Swift. Like we use #ifdef, #ifndef, #else, #endif in C (and C++, Objective C, ....)
I found the way to do it on the net, but I hit a problem in the following case. Anyone reading will understand what I want.
Nevertheless the compiler complains. What is the way to go around? Of course without having to copy two times the same ten or more lines.
#if UseAds
class ViewController: UIViewController,XYZBannerDelegateProtocol {
#else
class ViewController: UIViewController {
#endif
Note that I got the information I am using here:
http://en.swifter.tips/condition-compile/
which is similar to what can be found here.
But none of these solves my problem. They only tell me the basic way to do it.

You can use like this :
class ViewController: UIViewController {
// Your common functions
}
#if UseAds
extension ViewController: XYZBannerDelegateProtocol {
// Your delegate methods
}
#endif

Related

Objective C to Swift interoperability issues

I have a curious case where I have the following Objective C protocol:
NS_ASSUME_NONNULL_BEGIN
#protocol AccountCriteria <NSObject>
- (BOOL)criteriaIsApplicableForIdentifier:(NSString *)identifier;
- (nullable id <ModularFactory>)criteriaMetForAccount:(Account *)account error:(NSError **)error NS_SWIFT_NOTHROW;
#end
NS_ASSUME_NONNULL_END
To which I have decided to have a Swift class implement the protocol, like so:
import Foundation
#objc(PaymentCriteria)
public class PaymentCriteria: NSObject, AccountCriteria {
public func criteriaIsApplicable(forIdentifier identifier: String) -> Bool {
//Lots of code here
}
public func criteriaMet(for account: Account, error: NSErrorPointer) -> ModularFactory? {
//Lots of code here
}
}
The intention is to use the PaymentCriteria class in other Objective C code. However with this particular code I keep getting a Lexical or Preprocessor Issue. Now if I remove the public from the class everything builds fine. However, I can't use PaymentCriteria in any Objective C code.
If I keep the class as public but remove AccountCriteria from the class, everything still builds fine. However, I have to have this class abide by the protocol. So this route doesn't help me very much as well.
So my question is, why can't I have a Swift class abide by an Objective C protocol and then try and use that Swift class in Objective C? Or is there something pernicious that I'm doing wrong in the Swift class?
As I understood, the issue here with let's call it, cyclic importing.
So, You've created Objc protocol, then add this Objc file to Bridging header. It is being imported to all swift files, including your PaymentCriteria.swift file.
Then you try using PaymentCriteria back in objc, which leads to adding it to umbrella header. And generally because of such a path I experienced similar errors.
The simplest workaround would be creation AccountCriteria as a protocol using Swift with #objc.

Swift Protocol compilation error when using TARGET_INTERFACE_BUILDER

I am trying to generate sample data for an #IBDesignable control, so when building for IB I'm fooling the control into being its own datasource. The upshot is I'm adding some methods to a protocol only for use by IB, and as a good citizen I wish to remove these for a regular (non-IB build).
I've distilled my problem down to the following code fragment. My protocol looks like this:-
protocol TestProtocol {
#if TARGET_INTERFACE_BUILDER
func myControl(control:AnyObject, colorForIndex index:UInt) -> UIColor?
func myControl(control:AnyObject, textForIndex index:UInt) -> String?
#endif
}
This fails to compile, with a message that implies an method overloading error: "error: declaration conflicts with previous value". The error message is on the second function declaration, and refers to the first function as the previous declaration it's clashing with.
But these are not overloads, they have different signatures because of the named parameters. And this is such a standard delegate naming convention across Cocoa that I was resistant to renaming my methods without understanding why.
Removing the #if TARGET_INTERFACE_BUILDER fixes the problem, so it is no longer a pressing issue for me, but I am completely stumped as to why adding this conditional compilation would produce such a bizarre error?
I am not sure why it happens, but happen to find a reasonable workaround.
Just separate the two declarations as shown below:
protocol TestProtocol {
#if TARGET_INTERFACE_BUILDER
func myControl(control:AnyObject, colorForIndex index:UInt) -> UIColor?
#endif
#if TARGET_INTERFACE_BUILDER
func myControl(control:AnyObject, textForIndex index:UInt) -> String?
#endif
}

Module "MyApp" not found in UnitTest-Swift

Im trying to test some Swift class (#objc class) in my legacy Objc code. I am importing "UnitTests-Swift.h" in my test classes.
Then I get this error:
Module "MyApp" not found in the autogenerated "UnitTests-Swift.h"
This is whats inside the top part of the "UnitTests-Swift.h"
typedef int swift_int3 __attribute__((__ext_vector_type__(3)));
typedef int swift_int4 __attribute__((__ext_vector_type__(4)));
#if defined(__has_feature) && __has_feature(modules)
#import XCTest;
#import ObjectiveC;
#import MyApp;
#import CoreData;
#endif
I cleaned the project, checked all the relevant flags ("No such module" when using #testable in Xcode Unit tests, Can't use Swift classes inside Objective-C), removed derived data and so on.. No idea of whats happening, but I m quite sure that #import MyApp shouldnt be there..
Can anyone help me with this?
Just got this issue in my project and after the entire day spent on investigation I finally resolved it.
Basically this happens because you have cyclic dependency between ObjC and Swift. So in my case it was:
Swift Protocol with #obj attribute in a main target;
Swift Class in UnitTest target which inherited this Protocol;
Import UnitTestTarget-Swift.h in any Objective-C class of your UnitTest target
So fairly simple combination leads to this error. In order to fix this you want either:
simply make sure that your Swift Class in UnitTest target is private, so it won't get to UnitTestTarget-Swift.h
or
do not mark your original Swift Protocol as #objc, which will allow you to access your SwiftClass from all ObjectiveC test classes, but those classes won't have any idea about the Swift Protocol.
Because the Swift class to be tested is part of MyApp, you should be importing "MyApp-Swift.h" in the test classes instead of "UnitTests-Swift.h".
You can add a Swift unit test (just create a new unit file and change the extension by .swift).
From that unit test file you can use your Swift classes.
And you can also import that file from your Objective-C unit tests (and the other way around) using the test module bridging headers.
And this would be the default example for your Swift unit test file:
import XCTest
#testable import MyApp
class MyAppSwiftTests: XCTestCase {
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
func testExample() {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
}
func testPerformanceExample() {
// This is an example of a performance test case.
self.measure {
// Put the code you want to measure the time of here.
}
}
}
This solution helped me:
Open Tests Target Build Settings
Search for HEADER_SEARCH_PATHS
In the line called "Header Search Paths" set value $CONFIGURATION_TEMP_DIR/myProjectName.build/DerivedSources
Clean and Cmd+U again
Hope it helps!
Many thanks to this article.

How to access an internal Swift class in Objective-C within the same framework?

Working on a mixed framework. imported inside the Obj-C file but the internal classes are not visible, only the public ones.
The documentation clearly states the internal clasees should be available between Swift and Obj-C:
Importing Swift into Objective-C To import a set of Swift files in the same framework target as your Objective-C code, you don’t
need to import anything into the umbrella header for the framework.
Instead, import the Xcode-generated header file for your Swift code
into any Objective-C .m file you want to use your Swift code from.
Because the generated header for a framework target is part of the
framework’s public interface, only declarations marked with the public
modifier appear in the generated header for a framework target. You
can still use Swift methods and properties that are marked with the
internal modifier from within the Objective-C part of your framework,
as long they are declared within a class that inherits from an
Objective-C class. For more information on access-level modifiers, see
Access Control in The Swift Programming Language (Swift 2).
Code Sample (Create a new project with a framework)
// SwiftObject.swift
public class SwiftObject: NSObject {
public class func doSomething() {}
}
internal class YetAnotherSwiftObject: NSObject {
internal class func doSomething() {}
}
// SomeObject.m file
#implementation SomeObject
- (void)someMethod {
[SwiftObject doSomething];
}
- (void)someOtherMethod {
[YetAnotherSwiftObject doSomething]; // Use of undeclared identifier
}
#end
As indicated in the docs, declarations marked with internal modifier don't appear in the generated header, so the compiler does not know about them and thus complaints. Of course, you could send messages using performSelector approach, but that's not convenient and bug-prone. We just need to help the compiler know that those declarations are there.
First, we need to use #objc attribute variant that allows you to specify name for your symbol in Objective-C:
// SwiftObject.swift
#objc(SWIFTYetAnotherSwiftObject)
internal class YetAnotherSwiftObject: NSObject {
internal class func doSomething() {}
}
And then you just need to create #interface declaration with the methods you want to use in your code - so the compiler will be happy, and also apply SWIFT_CLASS macro with the symbol name you've specified earlier - so the linker would pick the actual implementation:
// SomeObject.m file
SWIFT_CLASS("SWIFTYetAnotherSwiftObject")
#interface YetAnotherSwiftObject : NSObject
+ (void)doSomething;
#end
#implementation SomeObject
- (void)someOtherMethod {
[YetAnotherSwiftObject doSomething]; // Should work now !!!
}
#end
I've used the interface declaration in .m file just for clarity, the better option would be to combine such declarations in .h file, and include it.
By declaring methods in that interface we're making a promise to compiler, and it won't complain if you'll put there a method that does not exist (or with wrong signature, etc.) Obviously, you'll crash in runtime in that case - so be cautious.
For me it just worked by checking: "Allow app extension API only". You find it by going to the project setting, select your target and then it is in the General tab under Deployment Info.
Can someone explain to me, why this does solve the problem?
While the above solution works (https://stackoverflow.com/a/33159964/5945317), it seems overly complicated and unintuitive:
Complicated, because it seems to add more things than necessary – I will provide a smoother solution below.
Unintuitive, because the objc macro SWIFT_CLASS resolves to SWIFT_RUNTIME_NAME, and the provided value is not actually the runtime name – nor is the objc class name in the header matching the Swift attribute param in #objc. Still, surprisingly, the solution works – but to me it is not clear why.
Here is what we have tested in our own project, and believe to be the better solution (using the example above):
// YetAnotherSwiftObject.swift
#objc(OBJCPREFIXYetAnotherSwiftObject)
internal class YetAnotherSwiftObject: NSObject {
#objc internal class func doSomething() {}
}
// OBJCPREFIXYetAnotherSwiftObject.h
#interface OBJCPREFIXYetAnotherSwiftObject : NSObject
+ (void)doSomething;
#end
That's it. The interface looks like a regular objc interface. This gives the added benefit that you can include it in other header files (which you cannot do if you use the SWIFT_CLASS macro, as it comes from the autogenerated Swift header file, which in turn you cannot include in an objc header, due to circular dependency).
On the Swift side, the only thing relevant is that you provide the class with the proper objc name. Mind that I only used the name prefix for language consistency – you can even just use YetAnotherSwiftObject everywhere (i.e., in the objc header and in the #objc attribute in Swift – but you need to keep this attribute with explicit naming in any case, and need to keep it consistent with the class name in the header).
This also makes your life easier if you're in the process of converting your objc framework step by step to Swift. You just keep the objc header as before, and now provide the implementation in Swift.
Methods and properties that are marked with the internal modifier and declared within a class that inherits from an Objective-C class are accessible to the Objective-C runtime.
so let's make use of that:
class MyInternalClass: NSObject {
#objc var internalProperty = 42
}
#interface MyPublicClass()
#end
#implementation MyPublicClass
+ (void) printValue {
Class myInternalClass = NSClassFromString(#"MyPackageNameIfAny.MyInternalClass");
id myInternalClassInstance = [myInternalClass new];
int value = [myInternalClassInstance performSelector:#selector(internalProperty)];
NSLog(#"Value is %d ", value); // "value is 42"
}
#end
Using the SWIFT_CLASS macro and #objc class attribute could easily lead to errors when archiving. This approach is safer.

I want to call a class function written in Swift from the AppDelegate (which is in Objective C). What am I doing wrong?

I have a swift file "SomeController.swift" it is like this:
import Foundation
func performSomeStuff() {
println("Performing stuff")
}
Now in the app delegate, I am trying to do this: (note that the swift bridging header is imported)
[SomeController performSomeStuff]
But its not working.
I have also tried this:
import Foundation
class SomeController:NSObject {
class func performSomeStuff() {
println("Performing stuff")
}
}
But it still fails.
What is the correct way?
Add:
#objc
before the class keyword in your swift code so it will be:
#objc class SomeStuff: NSObject {
}
Also add #obj in front of any function that you want to call.
Then in your app delegate make sure to use #import "projectName-Swift.h"
Are you able to access "SomeController" class in objective-c, if not then you firstly need to add "${ProjectName}-Swift.h file and add Swift compilation support in Build settings as:
And for accessing methods from Swift to Objective-C, add
import Foundation
class SomeStuff:NSObject {
#objc class func performSomeStuff() {
println("Performing stuff")
}
}
before functions name.
In some cases, you need finer grained control over how your Swift API is exposed to Objective-C. You can use the #objc attribute if your Swift class doesn’t inherit from an Objective-C class, or if you want to change the name of a symbol in your interface as it’s exposed to Objective-C code.
https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithObjective-CAPIs.html
Ok, So I figured it out. First, here's the correct way. Its like some of you said:
In the swift file, I have it like this:
class MyController:NSObject{
class func performTask {
// Here my task is running.
}
}
Then in the app delegate, I just import the swift header file. And do this:
[MyController performTask];
Now for the part I had wrong. It was an error on my part, but maybe it'll be useful to someone else out there.
When I first created the first swift file, I had placed it inside a folder within the my Source folder. But when I started having multiple swift files, I moved the bridging header outside that folder and into the main Source folder (just for organising).
The problem was, it did not give me a direct error to tell me what was the problem. I had to check the issue navigator to identify the problem.
Hope this helps someone out there.

Resources