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
Copy file name to clipboardExpand all lines: README.md
+38Lines changed: 38 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1239,6 +1239,40 @@ _You can enable the following settings in Xcode by running [this script](resourc
1239
1239
1240
1240
</details>
1241
1241
1242
+
*<a id='prefer-for-loop-over-forEach'></a>(<a href='#prefer-for-loop-over-forEach'>link</a>) **Prefer using `for` loops over the functional `forEach(…)` method**, unless using `forEach(…)` as the last element in a functional chain. [](https://github.com/nicklockwood/SwiftFormat/blob/master/Rules.md#forLoop)
1243
+
1244
+
<details>
1245
+
1246
+
#### Why?
1247
+
For loops are more idiomatic than the `forEach(…)` method, and are typically familiar to all developers who have experience with C-family languages.
1248
+
1249
+
For loops are also more expressive than the `forEach(…)` method. For loops support the `return`, `continue`, and `break` control flow keywords, while `forEach(…)` only supports `return` (which has the same behavior as `continue` in a for loop).
1250
+
1251
+
```swift
1252
+
// WRONG
1253
+
planets.forEach { planet in
1254
+
planet.terraform()
1255
+
}
1256
+
1257
+
// WRONG
1258
+
planets.forEach {
1259
+
$0.terraform()
1260
+
}
1261
+
1262
+
// RIGHT
1263
+
for planet in planets {
1264
+
planet.terraform()
1265
+
}
1266
+
1267
+
// ALSO FINE, since forEach is useful when paired with other functional methods in a chain.
1268
+
planets
1269
+
.filter { !$0.isGasGiant }
1270
+
.map { PlanetTerraformer(planet: $0) }
1271
+
.forEach { $0.terraform() }
1272
+
```
1273
+
1274
+
</details>
1275
+
1242
1276
*<a id='omit-internal-keyword'></a>(<a href='#omit-internal-keyword'>link</a>) **Omit the `internal` keyword** when defining types, properties, or functions with an internal access control level. [](https://github.com/nicklockwood/SwiftFormat/blob/master/Rules.md#redundantInternal)
1243
1277
1244
1278
<details>
@@ -1564,6 +1598,8 @@ _You can enable the following settings in Xcode by running [this script](resourc
1564
1598
}
1565
1599
```
1566
1600
1601
+
</details>
1602
+
1567
1603
*<a id='anonymous-trailing-closures'></a>(<a href='#anonymous-trailing-closures'>link</a>) **Prefer trailing closure syntax for closure arguments with no parameter name.** [](https://github.com/nicklockwood/SwiftFormat/blob/master/Rules.md#trailingClosures)
1568
1604
1569
1605
<details>
@@ -1584,6 +1620,8 @@ _You can enable the following settings in Xcode by running [this script](resourc
1584
1620
planets.first { $0.isGasGiant }
1585
1621
```
1586
1622
1623
+
</details>
1624
+
1587
1625
### Operators
1588
1626
1589
1627
*<a id='infix-operator-spacing'></a>(<a href='#infix-operator-spacing'>link</a>) **Infix operators should have a single space on either side.** Prefer parenthesis to visually group statements with many operators rather than varying widths of whitespace. This rule does not apply to range operators (e.g. `1...3`) and postfix or prefixoperators (e.g. `guest?` or `-1`). [](https://realm.github.io/SwiftLint/operator_usage_whitespace)
0 commit comments