Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

Commit 773a6b7

Browse files
author
Kerr Marin Miller
committed
Update styleguide for swift optionals
1 parent 56cb31c commit 773a6b7

File tree

1 file changed

+34
-13
lines changed

1 file changed

+34
-13
lines changed

swift/README.md

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ Prefer interpolation over concatenation:
99

1010
```swift
1111
let name = "John"
12-
let messsage = "Hello \(name)"
12+
let messsage = "Hello \(name)" //YES
13+
let greeting = "Hello " + name //NO
1314
```
1415

1516
Optionals
@@ -19,21 +20,46 @@ Use `as` for type coercion if possible (this is enforced statically). Otherwise
1920

2021
DO NOT use `as!` or `value!` because Xcode told you so. Stop. Think. You probably want to use `if let value = value`.
2122

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:
2326

2427
```swift
25-
let regex = NSRegularExpression(pattern: "[a-z]", options: NSRegularExpressionOptions.allZeros, error: nil)!
28+
let numberSeven = Int("7")! //Don't do this.
2629
```
2730

28-
init() example:
31+
In this case, wrap this into a static function and write a test for it:
32+
2933
```swift
30-
class Component {
34+
extension Int {
35+
static func seven() -> Int {
36+
return Int("7")!
37+
}
38+
}
39+
```
40+
41+
```swift
42+
import XCTest
43+
44+
class IntegerTests: XCTestCase {
45+
func testSeven() {
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+
class Component: ParentComponent {
3157
var controller: CustomViewController! // We want to use it as a non-optional but have to initialize after super.init()
32-
33-
init() {
58+
59+
override init() {
3460
// controller = CustomViewController(component: self) <-- compiler error
3561
super.init()
36-
controller = CustomViewController(component: self)
62+
controller = CustomViewController(component: self)
3763
}
3864
}
3965
```
@@ -43,11 +69,6 @@ How do I handle errors in Swift?
4369

4470
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.
4571

46-
You have a couple options:
47-
48-
- Return a tuple (similar to Go)
49-
- Use a result/error enumeration type
50-
5172
We typically use an enumeration type:
5273

5374
```swift

0 commit comments

Comments
 (0)