Roughly six hundred million people have asked me to write something about Swift. I’m hoping to write a series of articles about various aspects of it. For today, I want to start things off by exploring a few Swift features that are interesting and unusual from the perspective of Objective-C, and why I think they’re going to be good to have.
Swift makes optional values a first-class language construct, and requires that any optional type be explicitly marked as such. For example:
var a: Int var b: Int?
a is a plain
Int and it always contains some integer value.
b is an optional
Int and it either contains an integer value, or it contains nothing.
That example is not so special, but it gets more interesting with other types:
var c: String var d: String?
Just like above,
c is a plain
String and it always contains some string value.
d is an optional
String and it can either contain a string value or it can contain nothing.
Let’s look at the equivalent Objective-C declarations:
int e; NSString *f;