[UPDATE] This is actually a Swift 1.2 feature. My bad. It’s still nice.

Just last week, I made light of the amount of nesting sometimes required when doing optional checking in Swift code. The code in that image reads json and then creates a model object. Something like…

//Swift 1.0
if let identifier = dictionary["identifier"] as? String {
	if let name = dictionary["name"] as? String {
		if let description = dictionary["description"] as? String {
			let item = Item(identifier, name: name, description: description)
			//do something...
		}
	}
}

While the type checks and guaranteed values provide safety, this is an incredibly common pattern and a bit ungainly as the nesting gets deeper. Fortunately, Swift 2 1.2 adds a way to do these same checks in a much more concise manner.

A new feature of if statements allows multiple, comma separated, let declarations in the condition. The code sample above becomes…

//Swift 1.2
if let identifier = dictionary["identifier"] as? String,
   let name = dictionary["name"] as? String,
   let description = dictionary["description"] as? String {
	 let item = Item(identifier, name: name, description: description)
	 //do something...
}

Even in this simple example, two levels of nesting are removed by this small change while maintaining all safety checks. As an added bonus, this also can be used with the new guard keyword. It may be syntactic sugar, but at least it’s sweet.