Swift 2.1
If you thought Apple was slowing down with Swift, think again. Xcode 7.1 Beta 2 includes Swift 2.1. As always you can check the release notes for yourself because I don't necessarily address everything here.
Interop & Types
- Enums imported from C automatically conform to
Equatable
so now they work in pattern matching switch statements without forcing you to write your own extension and operator. - Named C unions now import as structs with fields corresponding to each field of the union. By using a struct Swift presumably preserves the ability to use a union to project a different view on the same underlying bits but I have yet to verify that Swift really does make the struct's fields use the same underlying storage.
- The relatively little-known C bitfield members of C structs are now imported and usable in Swift
dispatch_block_t
goes back to being@convention(block) () -> Void
sodispatch_block_create
works properly again.
Sweet Stuff
- 🎉 String interpolation now allows string literals. 🎉 This one bothered me constantly. Now you can do this:
"fancy \(thing ?? "")"
- If only
private
things are modified in a file then it does not trigger recompilation of all files that depend on it. In some cases this can have a huge performance benefit - Error messages produced by the type checker "continue to improve". I wouldn't pay this one much attention except they explicitly call out a really common scenario: a generic closure containing an error in its body. This used to almost always give you an error on the outer scope or statement that was useless; I've gotten used to cutting and pasting the bodies of closures to get around it or by manually giving the closure its argument and return types. This one is great news.
Covariance / Contravariance
Function/closure types now have covariance and contravariance.
This is a fancy way of saying that you can pass an Any -> Int
to something expecting a String -> Any
.
To put it another way, the parameters of your closure can be less specific than the target while the return type can be more specific than the target. This makes sense if you think about it... if my function accepts (Any, Any)
then surely it can accept (String, Int)
or (AnyObject, NSURLRequest)
. If the target is expected to produce AnyObject
then surely a UIButton
qualifies.
Bug Fixes
- A bug that could cause crashes or memory leaks when an Objective-C block was passed to a Swift method was fixed, though I haven't seen the details of when this might happen.
- Using
switch
against multiple types usingas Type
patterns no longer causes a memory leak - Declaring multiple global variables in a single
var
orlet
no longer causes memory corruption while let
andwhile case
no longer leak variables into the following scope; This would cause a compiler crash if used so I doubt this is a breaking change for anyone.
Known Issues
- Compiling with optimizations and debug information enabled at the same time can cause a compiler crash. Workaround: pass
-gnone
to disable debug information for non-debug builds.
Personal Notes
Things have been hectic with illnesses and a busy schedule but I'm looking forward to resuming blogging regularly. I am currently finishing off several posts that have been waiting for a while; some concern Swift and some are more general.
This blog represents my own personal opinion and is not endorsed by my employer.