Skip to content

Instantly share code, notes, and snippets.

@jonathan-beebe
Created December 22, 2016 15:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonathan-beebe/19eaf8e69f7d907e00ca637752ef953a to your computer and use it in GitHub Desktop.
Save jonathan-beebe/19eaf8e69f7d907e00ca637752ef953a to your computer and use it in GitHub Desktop.
Bridging an optional bool from Swift to Objective-C
@implementation MyObjcClass
. . .
if(mySwiftClassInstance.hasFeatureBridged == nil) {
// handle nil case
}
else if(mySwiftClassInstance.hasFeatureBridged boolValue] == YES) {
// handle true case
}
else {
// handle false case
}
. . .
@end
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)
}
. . .
}
@Porubay
Copy link

Porubay commented Aug 15, 2017

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment