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.

Apple has also revamped all of the Objective-C APIs that required NS_OPTIONS values to use the new OptionSetType. This causes breaking code changes, but continues to move Swift’s syntax forward along with the APIs that it supports.

The OptionSetType makes it easy to pass multiple settings as a single parameter, without requiring you to become an expert in binary arithmetic:

// You can create custom flags or options using OptionSetType
struct VideoFormat: OptionSetType {
   let rawValue: Int
   // Use static let values within the struct and assign
   // the raw value using bitmask values (1, 2, 4, 8, 16, etc)
   static let Video1080p = VideoFormat(rawValue: 1)
   static let Video720p = VideoFormat(rawValue: 2)
   static let EnableStreaming = VideoFormat(rawValue: 4)
   static let EnableDownloads = VideoFormat(rawValue: 8)
}
// Use Swift Set notation to pass options
func startVideoPlaybackWithOptions(videoStreamOptions: [VideoFormat]) {
   print("Play video stream:", videoStreamOptions)
}
// Store a collection of options using Set notation
let videoOptions = [VideoFormat.Video1080p, VideoFormat.EnableDownloads]
startVideoPlaybackWithOptions(videoOptions)
// Set notation makes legacy bitmask operations much easier to read
if videoOptions.contains(VideoFormat.Video1080p) {
   print("Video is 1080p!")
}
Download Swift Playground files

Modernized SDKs and Objective-C enhancements

The last time I wrote about Swift, I wasn’t sure if Apple would continue to support Objective-C with new features. With Swift 2, Apple has proven its commitment to Objective-C.

Enhancements to Objective-C have been made to directly support writing code in Swift that calls Objective-C APIs (most of Apple’s Cocoa Touch and Cocoa SDKs).

Apple added lightweight generics to Objective-C that allow collection types to store extra type information about the elements. This provides more type safety in Objective-C, but it also means that any API using a collection type passes on the same type safety when accessed from Swift code.

The touchesBegan(_:withEvent:) method from the UIResponder protocol in Swift now supports typed collections and has been remastered for Swift 2. Take a look at the Swift syntax and API evolution from Swift 1.1 to Swift 1.2 to Swift 2.1 as Apple released fallible initializers, the Swift Set collection type, and added Objective-C’s lightweight generics:

class ViewController : UIViewController {
   // Swift 1.1 (APIs used Objective-C NSSet type)
   override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
       if let touch = touches.anyObject() asUITouch? {
           let location = touch.locationInView(self.view)
           println("Touch: \(location)")
       }
   }
   // Swift 1.2 (Swift Set introduced along with new as? keyword)
   override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
       if let touch = touches.firstas? UITouch {
           let location = touch.locationInView(self.view)
           println("Touch: \(location)")
       }
   }
   // Swift 2.1 (Objective-C gains lightweight generics + Swift API update)
   override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
       if let touch = touches.first {
           let location = touch.locationInView(self.view)
           print("Touch: \(location)") // New print() method
       }
   }
}
Download Swift Playground files

 

Previous Page  1  2  3  4  5  6  7  Next Page 

Sign up for CIO Asia eNewsletters.