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
Update existing guideline to emphasize usage of "let" over "var"
- Update language to place more emphasis on using let
- Add "Preferred" and "Not Preferred" examples so that the guideline stands out
- Update a few other examples to use let instead of var
Copy file name to clipboardExpand all lines: README.markdown
+27-11Lines changed: 27 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -97,7 +97,7 @@ Swift types are all automatically namespaced by the module that contains them. A
97
97
```swift
98
98
importMyModule
99
99
100
-
var myClass = MyModule.MyClass()
100
+
let myClass = MyModule.MyClass()
101
101
```
102
102
103
103
You **should not** add prefixes to your Swift types.
@@ -338,9 +338,25 @@ In Sprite Kit code, use `CGFloat` if it makes the code more succinct by avoiding
338
338
339
339
### Constants
340
340
341
-
Constants are defined using the `let` keyword, and variables with the `var` keyword. Any value that **is** a constant **must** be defined appropriately, using the `let`keyword. As a result, you will likely find yourself using `let` far more than `var`.
341
+
Constants are defined using the `let` keyword, and variables with the `var` keyword. Always use `let`instead of `var` if the value of the variable will not change.
342
342
343
-
**Tip:** One technique that might help meet this standard is to define everything as a constant and only change it to a variable when the compiler complains!
343
+
**Tip:** A good technique is to define everything using `let` and only change it to `var` when the compiler complains!
344
+
345
+
**Preferred:**
346
+
```swift
347
+
let myClass =MyClass()
348
+
let sectionCount =5
349
+
let request =NSMutableURLRequest(URL: myURL)
350
+
request.HTTPMethod="GET"
351
+
```
352
+
353
+
**Not Preferred:**
354
+
```swift
355
+
var myClass =MyClass()
356
+
var sectionCount =5
357
+
var request =NSMutableURLRequest(URL: myURL)
358
+
request.HTTPMethod="GET"
359
+
```
344
360
345
361
### Optionals
346
362
@@ -392,13 +408,13 @@ Use the native Swift struct initializers rather than the legacy CGGeometry const
392
408
**Preferred:**
393
409
```swift
394
410
let bounds =CGRect(x: 40, y: 20, width: 120, height: 80)
395
-
var centerPoint =CGPoint(x: 96, y: 42)
411
+
let centerPoint =CGPoint(x: 96, y: 42)
396
412
```
397
413
398
414
**Not Preferred:**
399
415
```swift
400
416
let bounds =CGRectMake(40, 20, 120, 80)
401
-
var centerPoint =CGPointMake(96, 42)
417
+
let centerPoint =CGPointMake(96, 42)
402
418
```
403
419
404
420
Prefer the struct-scope constants `CGRect.infiniteRect`, `CGRect.nullRect`, etc. over global constants `CGRectInfinite`, `CGRectNull`, etc. For existing variables, you can use the shorter `.zeroRect`.
@@ -412,13 +428,13 @@ Prefer compact code and let the compiler infer the type for a constant or variab
412
428
**Preferred:**
413
429
```swift
414
430
let message ="Click the button"
415
-
var currentBounds =computeViewBounds()
431
+
let currentBounds =computeViewBounds()
416
432
```
417
433
418
434
**Not Preferred:**
419
435
```swift
420
436
let message: String="Click the button"
421
-
var currentBounds: CGRect =computeViewBounds()
437
+
let currentBounds: CGRect =computeViewBounds()
422
438
```
423
439
424
440
**NOTE**: Following this guideline means picking descriptive names is even more important than before.
@@ -482,12 +498,12 @@ The only exception to this rule is the `for-conditional-increment` construct, wh
482
498
483
499
**Preferred:**
484
500
```swift
485
-
var swift ="not a scripting language"
501
+
let swift ="not a scripting language"
486
502
```
487
503
488
504
**Not Preferred:**
489
505
```swift
490
-
var swift ="not a scripting language";
506
+
let swift ="not a scripting language";
491
507
```
492
508
493
509
**NOTE**: Swift is very different to JavaScript, where omitting semicolons is [generally considered unsafe](http://stackoverflow.com/questions/444080/do-you-recommend-using-semicolons-after-every-statement-in-javascript)
@@ -498,12 +514,12 @@ Use US English spelling to match Apple's API.
0 commit comments