Subscribe / Unsubscribe Enewsletters | Login | Register

Pencil Banner

Seven Swift 2 enhancements every iOS developer will love

Paul Solt | Feb. 2, 2016
Apple makes good on Swift’s emphasis on performance, approachability, and ease in latest update.

This enhancement reinforces one of Swift’s core strengths: It provides safety mechanisms to protect against invalid or optional values. Thanks to guard, your core app logic will be simpler to express (and maintain) because you will have blocked the invalid state in advance.

defer keyword

Swift 2’s new defer keyword adds a level of control flow that didn’t previously exist in Swift. The defer keyword allows you to easily clean up an expensive or limited resource. For example, if you are making the next Dropbox app in which you are reading and writing thousands of files, you would hit the maximum number of file descriptors for your app’s process if your code fails to close files after reading and writing completes. Using the defer statement, you can be sure that all files are closed either when the file processing completes or if an error occurs.

The key strength of defer is that it allows you to keep related code together, rather than having to split code into segments at the top and bottom of a method. Cleanup code (file close) can be written directly below the creation of an expensive or limited resource (file open) in a defer closure that will execute at the end of the current scope:

func fileProcessing() {
   print("1. Create file descriptor #1 and start file processing") 
   defer {
       print("5. Close and cleanup file descriptor #1 (I/O resource)")
   print("2. Create file descriptor #2 and start file processing")
   defer {
       print("4. Close and cleanup file descriptor #2 (I/O resource)")
   print("3. Finish file processing")
// Look at Xcode's Console output for order: 1, 2, 3, 4, 5.
Download Swift Playground files

In the previous code block, the messages will print to the Xcode console in the order 1, 2, 3, 4, 5. The defer keyword causes your code in the defer block to execute after the current scope’s closing curly bracket.

Multiple defer code blocks can be added inside your methods and functions. Each defer closure will execute in the reverse order it is called -- this ensures cleanup safety when combined with the new error handling model in Swift 2.

repeat/while and do scope

Apple recently made a surprise change to Swift’s control flow: The do/while loop is now the repeat/while loop.

It’s a small change, and it goes against the convention of established languages like Objective-C, JavaScript, C++, and Java. The rationale is that the repeat keyword provides an instant cue that the block of code will repeat, enhancing code readability:

// A method that counts down and outputs to Xcode’s Console
func repeatWhile() {
   var x = 10
   repeat {
       print("T-minus", x, "seconds")
       x = x - 1// x-- is being deprecated in Swift 3.0!
   } while x > 0
   print("Blast off!")
// Call the method in Playgrounds
Download Swift Playground files


Previous Page  1  2  3  4  5  6  7  Next Page 

Sign up for CIO Asia eNewsletters.