-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample.swift
65 lines (49 loc) · 1.42 KB
/
sample.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Don't include generated header comments
import Foundation // or not
// MARK: Closures
// Use typealias when closures are referenced in multiple places
typealias CoolClosure = (foo: Int) -> Bool
// Use aliased parameter names when function parameters are ambiguous
func yTown(some: Int, withCallback callback: CoolClosure) -> Bool {
return CoolClosure(some)
}
// It's OK to use $ variable references if the closure is very short and
// readability is maintained
let cool = yTown(5) { $0 == 6 }
// Use full variable names when closures are more complex
let cool = yTown(5) { foo in
if foo > 5 && foo < 0 {
return true
} else {
return false
}
}
// MARK: Optionals
var maybe: Bool?
// Use if-let syntax to unwrap optionals
if let definitely = maybe {
println("This is \(definitely) here")
}
// If the API you are using has implicit unwrapping you should still use if-let
func someUnauditedAPI(thing: String!) {
if let string = thing {
println(string)
}
}
// MARK: Enums
enum Response {
case Success(NSData)
case Failure(NSError)
}
// When the type is known you can let the compiler infer
let response: Response = .Success(NSData())
func doSomeWork() -> Response {
let data = ...
return .Success(data)
}
switch response {
case let .Success(data):
println("The response returned successfully \(data)")
case let .Failure(error):
println("An error occured: \(error)")
}