Whats New in Swift (106)
- print out debug values
- supports associated types
- recursive enum
Introduces a explicit scope, helps with error. do/while -> repeat
- representing sets of booleans, can do bitwise operations
- can now be used as a set
[.Repeat, .CurveEaseIn]
- Can create your own option set, create a struct implementing OptionSetType (implemented through default implementation in protocols!)
Both work the same way in terms of argument labels/defaults, no change for functions imported from C. Argument labels are expected as part of pure swift api for functions now. Can customize argument label
func save(name name : String, encrypt : Bool) // save(name: “foo”, encrypted: true)
func save(name : String, _ encrypt : Bool) // save(“foo”, true)
Better error messages around mutability safety. Other new warnings
Better integration of swift and objective-c. Adoption of best practices on obj-c code, nullability qualifiers, objective-c typed collections, NS_ENUM, NS_Options, etc…
Unit tests use @testable, public and internal symbols now available, same optimizations/collision prevention exist in release builds
Documentation comments can include markup like syntax
great for early exits, your else exits the current scope (return, break, abort, throw), ensures that after the fall through compiler can assume the inverse
guard let name = json[“name”] as? String { return nil } name.doSomething() // no optional
if case .MyEnumCase(let value) = bar() where value != 42 {}
for value in mySequence where value != “” {}
- Compiler now warns on apis that you call not available on min deployment target
if #available(OSX 10.10.3, *) { // run if 10.10.3 }
Can add extensions to protocols, provide implementation for methods
Lots of global functions are becoming methods:
filter(map(numbers) {$0 * 3}) { $0 >= 0}
numbers.map({$0 * 3}).filter({$0 >= 0})
`try`
by default functions can’t throw, makes explicit what can with `throws` keyword in method
`do`/`catch` pattern match against protocol ErrorType (NSError conforms)
`catch let error` global catch and assign to error
`catch NSURLError.somelongErrorType` catch one error type
Impossible errors use `try!`, will abort execution if the code throws