Last active
May 18, 2016 13:16
-
-
Save dominickm/5833eed373426385e555c314a4604793 to your computer and use it in GitHub Desktop.
This file contains 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
// This is meant to go along with Coder Radio Episode 204 | |
// Optional types in Swift 101 | |
struct JarJar { | |
var senateSpeach: String? // the '?' means messa might be nil and makes me an Optional type | |
func voteOfNoConfidence() { | |
// attempting to use the senateSpeach var but not setting it's value | |
let isSith = senateSpeach?.containsString("something something darkside") // senateSpeach is nil | |
// the '?' let's the compiler know that we're not sure about that slippery senateSpeech, so if it's nil this won't crash | |
// we should note that isSith is a Bool but doesn't default ot false in this case, instead it's nil / 'invalid' | |
} | |
func insaneVoteOfConfidence() { | |
// attempting to use the senateSpeach var but not setting it's value | |
let isSith = senateSpeach!.containsString("messa gonna burn this mother down!") // senateSpeach is still nil... | |
// the '!' let's the compiler now that we are 'SURE' that senateSpeach isn't nil -- this is called force unwrapping | |
// this is suicide... we are force unwrapping an optional that we have no reason to think or guarantee that it's not nil | |
// this code will crash with: "fatal error: unexpectedly found nil while unwrapping an Optional value" | |
// in general force unwrapping should be done sparingly and only if you're 100% sure that the value isn't nil | |
} | |
} |
This file contains 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
// this is the rough ObjC equivelant to the Swift above. I have ignored a header and truncated some code for brevity | |
// for the purposes of this sample we'll ignore the differences between Swift String and NSString | |
@interface JarJar() | |
@property(nonatomic, weak) NSString *senateSpeach; | |
@end | |
@implementation JarJar | |
- (void)voteOfNoConfidence() { | |
// to get the nil protection '?' in Swift gives, we'd need to manually implement a nil check | |
if(_senateSpeach != nil) { | |
BOOL isSith = [_senanteSpeach containsString:@"something something darkside"]; | |
} | |
} | |
- (void)insaneVoteOfConfidence() { | |
// '!' is Swift has the same effect as just going sans a nil check in ObjC | |
BOOL isSith = [_senanteSpeach containsString:@"something something darkside"]; | |
// this crashes and burns | |
} | |
@end |
When you have to do a lot of parsing, where some values will inevitably be null (or "nil"), this is a very useful feature, especially if you have some logic taking care of the nulls. I'm glad C# 6.0 got this as well.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So the objc version
isSith
is not defined if_senateSpeach == nil
. From reading the swift version ifsenateSpeach == nil
thenisSith
would be defined and assigned the value ofnil
. A bit of a difference. I don't know how much of a fan I am of optionals. I haven't written a lot of swift but I could see it leading to unintendednil
propagation.Also, more coding examples on the show! and Jar Jar references!