Skip to content

Commit 4f60c0f

Browse files
committed
Type inference examples
1 parent 26216e6 commit 4f60c0f

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

README.markdown

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -441,20 +441,21 @@ Prefer the struct-scope constants `CGRect.infiniteRect`, `CGRect.nullRect`, etc.
441441

442442
### Type Inference
443443

444-
The Swift compiler is able to infer the type of variables and constants. You can provide an explicit type via a type alias (which is indicated by the type after the colon), but in the majority of cases this is not necessary.
445-
446-
Prefer compact code and let the compiler infer the type for a constant or variable.
444+
Prefer compact code and let the compiler infer the type for a constant or variable, unless you need a specific type other than the default such as `CGFloat` or `Int16`.
447445

448446
**Preferred:**
449447
```swift
450448
let message = "Click the button"
451449
let currentBounds = computeViewBounds()
450+
var names = [String]()
451+
let maximumWidth: CGFloat = 106.5
452452
```
453453

454454
**Not Preferred:**
455455
```swift
456456
let message: String = "Click the button"
457457
let currentBounds: CGRect = computeViewBounds()
458+
var names: [String] = []
458459
```
459460

460461
**NOTE**: Following this guideline means picking descriptive names is even more important than before.
@@ -479,7 +480,6 @@ var faxNumber: Optional<Int>
479480
```
480481

481482

482-
483483
## Control Flow
484484

485485
Prefer the `for-in` style of `for` loop over the `for-condition-increment` style.

0 commit comments

Comments
 (0)