Thursday, July 23, 2015

Swift Fun Facts

Fact 1: Assigning a variable to itself is a compiler error.  I believe this is legal in Swift, but the LLVM compiler (Xcode 6 and 7) doesn't allow it.
var i = 0
i = i     <--- compiler gives error: "Assigning variable to itself"

Now why you would want to do this is another question entirely.

Fact 2: Trying to use Swift's print() statement inside an extension or subclass of an NSView brings up the print panel.  Despite having different argument lists, Xcode and the LLVM compiler would only recognize the NSView method print.  Swift's print was totally masked.  To access Swift's print function, preface it with:
Swift.print()

Fact 3: In strongly typed situations, Swift allows you to use just the enum member name without having to also use the enum type name.  For example:
enum CompassPoint {
    case North, South, East, West
}
var pointing: CompassPoint
pointing = .East  

I didn't have to use "CompassPoint.East".  This is great for readability and for saving time.  Now if Xcode's autocomplete would just get with the program in these strongly typed situations, I wouldn't have to type the enum name just to get it to list the members.  Feel free to duplicate this Apple Bug Reporter radar (rdar://21976034)

Opinion 1
Favorite Swift 2 feature: Protocol extensions and constraints
Welcome to the age of Protocol Oriented Programming