BT

Apple Prepares Swift 1.2 For Release

| by Alex Blewitt Follow 4 Followers on Feb 10, 2015. Estimated reading time: 3 minutes |

Apple has announced Swift 1.2 as part of Xcode 6.3, with detailed release notes (sign-in required) indicating the changes to the language. Swift was originally announced during WWDC in June 2014, but the Swift 1.0 master was not released until September 2014, with a release of Swift 1.1 following shortly after in October 2014. Although this is available in beta form at present, it is likely to be relesaed to co-incide with the release of the Apple Watch in the next month or two. This will allow time for developers to migrate their code to Swift 1.2 syntax ahead of the general release.

The performance of Swift has been optimised further; the speed of computation for compiled code with optimisations turned on has improved, and debug builds (those without optimisations) have been improved significantly. David Owens, who originally posted some damning benchmarks on the performance of Swift code, was pleasantly surprised at the improvments.

There have been a few breaking changes in Swift code, and to mitigate against that Xcode 6.3 has a Edit > Convert > To Swift 1.2 menu option that will perform automatic convertion where necessary of items such as the as operator (for casts that cannot fail) and the as! operator (for casts that might fail at runtime which will fail). In addition, Objective-C container types (such as NSDictionary and NSArray) are no longer automatically cast to their Swift counterparts – an explicit nsdict as Dictionary is now required. (The reverse conversion is still applied automatically.) There is a new Set type in Swift, which bridges to the NSSet type in Objective-C. Uses of types like Byte have been replaced with UInt8.

The performance and interoperability with Objective-C has been improved. Arguments which may be null or return values which may be null can be annotated in Objective-C using new nullable and nonnull attributes, which correspond to optional or non-optional types in Swift. In addition, Swift enums are now exportable to Objective-C using the @objc attribute, which combines both the Enum name and also the value as typedef'd constants in Objective-C. Only integral enums are permitted; other values and associated data are not. For example, the following Swift can be used in Objective-C as:

Swift Objective-C
@objc enum Direction: Int {
  case North, South, East, West
}
typedef NS_ENUM(NSInteger,Direction) {
  DirectionNorth,
  DirectionSouth,
  DirectionEast,
  DirectionWest
};

The let and if let statements became more powerful as well; originally, Swift required let (constant) values be initialised directly upon creation, but this rule has been relaxed to ensure that the constant is defined before it is used. As a result, it is now possible to write code like:

let dir:Direction
if random() % 2 == 0 {
  dir = North
} else {
  dir = South
}

Additionally, if let statements now allow for multiple variables to be assigned in one go, as opposed to requiring nested if let statements:

var first: String?
var last: String?
...
if let f = first, l = last {
  println("Hello \(f) \(l)")
}

The build time for Swift code has also improved, in part due to not recompiling source files that have not changed and using incremental compilation on those that have changed. After an initial build, subsequent changes will be compiled much faster. The blog also promises less "SourceKit errors", which have occasionally required reboots before Xcode becomes responsive when compiling or using Swift code. Finally, over 80% of crashes recorded by Practical Swift have been resolved.

The release date of Swift 1.2 and its corresponding Xcode is not published at this time, but others have been working on Silver, an implementation of the Swift language that can target Java, .Net and Android devices. There is still no news on when or if Swift will be open sourced, but it certainly has momentum.

Rate this Article

Adoption Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss
BT