forked from thoughtbot/guides
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample.swift
128 lines (96 loc) · 2.95 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Don't include generated header comments
import Foundation // or not
// MARK: Types
// Prefer structs over classes
struct User {
let name: String
}
// When using classes, default to marking them as final
final class MyViewController: UIViewController {
// Prefer strong IBOutlet references
@IBOutlet var button: UIButton!
}
// 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
}
}
// Strongify weak references in async closures
APIClient.getAwesomeness { [weak self] result in
guard let `self` = self else { return }
self.stopLoadingSpinner()
self.show(result)
}
// MARK: Optionals
var maybe: Bool?
// Use if-let syntax to unwrap optionals
if let definitely = maybe {
print("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 {
print(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):
print("The response returned successfully \(data)")
case let .Failure(error):
print("An error occured: \(error)")
}
// Group methods into specific extensions for each level of access control
private extension MyClass {
func doSomethingPrivate() { }
}
// MARK: Capitalization
// Types begin with a capital letter
struct DopeObject {
// if the first letter of an acronym is lowercase, the entire thing should
// be lowercase
let json: AnyObject
// if the first letter of an acronym is uppercase, the entire thing should
// be uppercase
static func decodeFromJSON(json: AnyObject) -> DopeObject {
return DopeObject(json: json)
}
}
// Use () for void arguments and Void for void return types
let f: () -> Void = { }
// MARK: Guards
// One expression to evaluate and short or no return
guard let singleTest = somethingFailable() else { return }
guard statementThatShouldBeTrue else { return }
// If there is one long expression to guard or multiple expressions
// move else to next line
guard let oneItem = somethingFailable(),
let secondItem = somethingFailable2()
else { return }
// If the return in else is long, move to next line
guard let something = somethingFailable() else {
return someFunctionThatDoesSomethingInManyWordsOrLines()
}