1 swift 2.0: new features dzianis astravukh august 5, 2015
Post on 08-Jan-2018
222 Views
Preview:
DESCRIPTION
TRANSCRIPT
1
SWIFT 2.0:New featuresDzianis Astravukh
AUGUST 5, 2015
2
SWIFT 2.0
WWDC 2015
3
Swift 2.0: What’s new
“Guard” Statement1
Error Handling2
“Defer” Statement3
Protocol Extension4
4
“GUARD” STATEMENT
5
“Guard” statement
“A guard statement is used to transfer program control out of a scope if one or more conditions aren’t met.”
guard condition else {statements
}
6
Hey! I have already “if .. else” construction. Why do I need one more?
7
“Guard” statement
func fooBinding(x: Int?) {if let x = x where x > 0 {
// Do stuff with xx.description
}
// Value requirements not met, do something}
8
“Guard” statement
func fooGuard(x: Int?) {guard let x = x where x > 0 else {
// Value requirements not met, do something
return}
// Do stuff with xx.description
}
9
“Guard” statement
10
“Guard” statement
11
ERROR HANDLING
12
Should I really handle all exceptional cases in application?It’s just such pain is the ass…
13
Error handling in the past
• In Objective-C there main error handling tool was NSError• Exceptions were used to filter “programmer errors” and nobody really cared about catching these
14
ErrorType in Swift
• In Swift there is a special type (protocol) of enumeration which can be used to represent errors: ErrorType
enum VendingMachineError: ErrorType { case InvalidSelection case InsufficientFunds(required: Double) case OutOfStock }
15
Throwing errors
• To indicate that a given function or method can fail, it has to be marked with the throws keyword
• Whenever a method/function is called that can throw an ErrorType it has to be used with try in front of it. As an alternative you can use try! to stop error distribution
func vend(itemNamed name: String) throws {guard var item = inventory[name] else {
throw VendingMachineError.InvalidSelection
}…
}
16
Catching Errors
• To catch errors, you can use the do {...} catch {...} control flow
• Catch can be defined for specific ErrorTypes • If you don’t catch every possible ErrorType, than
every un-handled error type will be thrown forward (travels up on the call stack)
do {try vend(itemNamed: "Candy Bar")
} catch VendingMachineError.InvalidSelection {print("Invalid Selection.")
}
17
Benefits
• Since Swift has a strict type system (not even nil allowed for non optionals) you don’t have to declare a return value as an optional just because the function can fail
• Error distribution is a lot easier (try, catch, throw)
• You don’t have to use NSError** any more or create a delegate to notify about errors
18
“DEFER” STATEMENT
19
“Defer” statement
• Defer blocks can be used to delay the execution of a code block until it’s scope is executed • It is not strictly related to the new error handling flow. It can be used for many reasons. It’s a method for
cleaning up, even if your code failed at some point
// Some scope: {
// Get some resource. defer { // Release resource. } // Do things with the resource. // Possibly return early if an error occurs.
} // Deferred code is executed at the end of the scope.
20
PROTOCOL EXTENSIONS
21
Protocol extensions
let ints = [1,2,3,4,5]
//Swift 1map(filter(ints, {$0 % 1 == 0}), {$0 + 1})
//Swift 2ints.map({$0 + 1}).filter({$0 % 1 == 0})
22
Protocol extensions
extension CollectionType where Generator.Element : Equatable { public func indexOf(element: Generator.Element) -> Index? { for i in self.indices { if self[i] == element { return i } } return nil }}
23
Protocol extensions
24
Protocol extensions
protocol ImportantProtocol {func doImportantStuff()
}
//default implementation for functionextension ImportantProtocol {
func doImportantStuff() {print("doing important
stuff")}
}
25
PROTOCOL-ORIENTED PROGRAMMING
Ha? What does it mean? What about objects and classes?
26
GOING TO OPEN-SOURCEHow it will influence language growth
27
THANKS FOR YOUR ATTENTION!
top related