Created
December 22, 2016 15:41
-
-
Save jonathan-beebe/19eaf8e69f7d907e00ca637752ef953a to your computer and use it in GitHub Desktop.
Bridging an optional bool from Swift to Objective-C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@implementation MyObjcClass | |
. . . | |
if(mySwiftClassInstance.hasFeatureBridged == nil) { | |
// handle nil case | |
} | |
else if(mySwiftClassInstance.hasFeatureBridged boolValue] == YES) { | |
// handle true case | |
} | |
else { | |
// handle false case | |
} | |
. . . | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MySwiftClass: NSObject { | |
/// Can’t be seen by Objective-C because it can’t represent a nil value. | |
private(set) var hasFeature: Bool? | |
/// Can be seen by Objective-C because it can represent a nil object. | |
var hasFeatureBridged: NSNumber? { | |
guard let feature = hasFeature else { return nil } | |
return NSNumber(value: feature) | |
} | |
. . . | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi everyone. I have a question, I want to program iOS applications, so I have a question, how to do this, by learning Swift or by learning objective C? I read this so I am a little bit confused, what to learn?