You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
Copy file name to clipboardExpand all lines: swift/README.md
+34-13Lines changed: 34 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,8 @@ Prefer interpolation over concatenation:
9
9
10
10
```swift
11
11
let name ="John"
12
-
let messsage ="Hello \(name)"
12
+
let messsage ="Hello \(name)"//YES
13
+
let greeting ="Hello "+ name //NO
13
14
```
14
15
15
16
Optionals
@@ -19,21 +20,46 @@ Use `as` for type coercion if possible (this is enforced statically). Otherwise
19
20
20
21
DO NOT use `as!` or `value!` because Xcode told you so. Stop. Think. You probably want to use `if let value = value`.
21
22
22
-
You should use `!` only if you just assigned to an object, you know initialization can not fail, or you are initializing an object during init() but need to pass `self` to another object's init():
23
+
You should use `!` only if:
24
+
25
+
- You just assigned to an object where you know initialization can not fail:
In this case, wrap this into a static function and write a test for it:
32
+
29
33
```swift
30
-
classComponent {
34
+
extensionInt {
35
+
staticfuncseven() ->Int {
36
+
returnInt("7")!
37
+
}
38
+
}
39
+
```
40
+
41
+
```swift
42
+
importXCTest
43
+
44
+
classIntegerTests: XCTestCase {
45
+
functestSeven() {
46
+
XCTAssertNotNil(Int.seven())
47
+
}
48
+
}
49
+
```
50
+
51
+
Although the test might seem redundant, because `seven()` returns a non-optional, this ensures that even if in the future someone changes the implementation of `seven()`, it won't get past CI if the explicit unwrapping would fail.
52
+
53
+
- You are creating an object during initialization but need to pass `self` to that object's initializer:
54
+
55
+
```swift
56
+
classComponent: ParentComponent {
31
57
var controller: CustomViewController!// We want to use it as a non-optional but have to initialize after super.init()
@@ -43,11 +69,6 @@ How do I handle errors in Swift?
43
69
44
70
As of Swift 2.0 exceptions have been introduced into the language and the APIs that formerly used `someMethod(..., error: *NSError)` make use of them. We have decided to stick with our existing pattern for returning errors.
0 commit comments