Chris Lattner 實在是太帥了!
Session 106 - What’s new in swift
Why Swift 2
- Fundamentals: Refine language and tools fundamentals
- Safety: Affordances for writing robust and safe code
- Beauty: Enable expressive libraries and APIs
用更好的語言寫更加安全和漂亮的代碼!
What’s New in Swift 2
- Fundamentals
- Pattern Matching
- Availabilty Checking
- Protocol Extensions
- Error Handling
Fundamentals
Pattern Matching
if-let Statment -
Switch
if case .MyEnumCase(let value) = bar() where value != 42 {
doSomething(value)
}
-
for ... in Filtering
for value in mySequence where value != "" {
doSomething(value)
}
Availabilty Checking
- New APIs
- springLoaded
- old:
respondsToSeletor Method - new: get a diagnostic
-
Example:
if #available(OSX 10.10.3, *) {
...
}
Protocol Extensions
- Extensions
-
Old
extension Array {
func countIf(match: Element -> Bool) -> Int {
var n = 0;
for value in self {
if match(value) {
n++;
}
}
return n
}
} + Global Functions
-
New
-
Method
Error Handling
- Kinds
- Trivial Errors
- Logic Errors
- Detailed, recoverable Errors
- Example
try Keyword throws Keyword -
do-catch Block
do {
...
} catch let error {
...
} catch {
fatalError()
}
try! Keyword
-
Enums as Error Types
enum DataError : ErrorType {
case MissingName
case MissingFile
}
-
Defer Actions
defer { delegate?.didEndProcessing() }