18. Branching
switch numberOfApples
{
case 0:
return "none"
case 6...10:
return "just right"
case 42, -1:
return "let me see"
case let value where (1 == value % 2):
return "that's odd"
default:
return "this is unacceptable"
} 18
39. Imperative Style
let names = ["Luke", "Chuwy", "Baker"]
var messages = [String]()
for name in names {
let message =
buildMessageForPerson(name, "father")
messages.append(message)
}
print("(messages)")
39
40. Functional Style
let names = ["Luke", "Chuwy", "Baker"]
let messages = names.map {
buildMessageForPerson($0, "father")
}
print("(messages)")
40
41. Closures
func buildOperation(multiplier: Int
) -> (Int -> Int) {
func multiply(value: Int) -> Int {
return value * multiplier
}
return multiply
}
let operation = buildOperation(3)
let value = operation(4)
41
44. Type System Classification Criteria:
Parameters Passing and Ownership
• By Value
let a = 3
• By Sharing (by reference to shared object)
let userDefaults =
NSUserDefaults.standardUserDefaults()
• By Reference
let operation = myFunction
let c = operation(a, b)
44
46. Type System Classification
Criteria: Access Control
• Public - visible for importers
• Internal (default) - visible within module
• Private - visible within file
46
70. Enums (Unions)
switch fileSystemError {
case .InvalidURL(let URL):
print("Invalid URL (URL)")
case .InvalidPermissions:
print("Invalid permissions")
case .UnknownErrorCode(let code)
where code == -150:
print("I've seen this error somewhere")
case .UnknownErrorCode(let code):
print("Something gone very wrong (code)")
}
70
71. Protocols
protocol Drawable {
var boundingRect: NSRect { get }
func draw()
}
struct Polygon: Drawable
{
var points = [NSPoint]()
//MARK: Drawable
var boundingRect: NSRect? { ... }
func draw() { ... }
} 71