Swift 1.2
Some overlooked goodies
Apple has delivered on their promise to be, well, swift with Swift. Version 1.2 has been released and includes a number of updates and syntax changes.
Some are less welcome, like requiring parameter names for closures in methods with optional parameters. Others are great additions like Set
or the ability for value-returning closures to satisfy a Void
closure requirement.
One overlooked feature is @noescape
. This attribute indicates that a closure will only live as long as the call so there is no possibility of retain cycles. Besides allowing some optimizations, it means custom syntax-like functions no longer break the illusion by requiring everything to be qualified with self
. It may be minor but it just makes these functions feel much more natural:
func synchronized(context:AnyObject, @noescape f:()->Void) {
objc_sync_enter(context)
f()
objc_sync_exit(context)
}
func synchronized<T>(context:AnyObject, @noescape f:()->T) -> T {
objc_sync_enter(context)
let result = f()
objc_sync_exit(context)
return result
}
@objc class Wat : NSObject {
private var _bucket = [Int]()
func ok() {
synchronized(self) {
_bucket.append(5) //no self qualifier!
}
}
}
Aside: I think I've decided that Void
is preferable to ()
. It just keeps the parenthesis from multiplying uncontrollably.
This blog represents my own personal opinion and is not endorsed by my employer.