Skip to content

Commit 9e7377c

Browse files
authored
Merge pull request #34 from Biinngg/master
Updating the swift-style-guide to follow the official swift-format styles
2 parents c15058f + 35a9f2d commit 9e7377c

File tree

1 file changed

+45
-15
lines changed

1 file changed

+45
-15
lines changed

README.md

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,47 @@ This guide was last updated for Swift 4.0 on February 14, 2018.
3333
* **1.2** Avoid uncomfortably long lines with a hard maximum of 160 characters per line (Xcode->Preferences->Text Editing->Page guide at column: 160 is helpful for this)
3434
* **1.3** Ensure that there is a newline at the end of every file.
3535
* **1.4** Ensure that there is no trailing whitespace anywhere (Xcode->Preferences->Text Editing->Automatically trim trailing whitespace + Including whitespace-only lines).
36-
* **1.5** Do not place opening braces on new lines - we use the [1TBS style](https://en.m.wikipedia.org/wiki/Indentation_style#1TBS).
36+
* **1.5** For single-line statements, keep the opening braces on the same line. For multi-line statements, place the closing parentheses and opening braces on separate lines. This adheres to the default style defined by [swift-format](https://github.com/swiftlang/swift-format).
3737

3838
```swift
3939
class SomeClass {
40-
func someMethod() {
40+
// For single-line statements, keep the opening braces on the same line.
41+
func someMethod(firstArgument: String, secondArgument: String) -> String {
4142
if x == y {
4243
/* ... */
4344
} else if x == z {
4445
/* ... */
4546
} else {
4647
/* ... */
4748
}
49+
guard let firstInt = firstArgument.intValue else {
50+
return "..."
51+
}
52+
return "..."
4853
}
4954

50-
/* ... */
55+
// For multi-line statements, place the closing parentheses and opening braces on separate lines.
56+
func anotherMethod(
57+
firstArgument: String,
58+
...
59+
lastArgument: String
60+
) -> String {
61+
if let firstInt = firstArgument.intValue,
62+
...,
63+
let lastInt = lastArgument.doubleValue
64+
{
65+
/* ... */
66+
} else {
67+
/* ... */
68+
}
69+
guard let firstInt = firstArgument.intValue,
70+
...,
71+
let lastInt = lastArgument.doubleValue
72+
else {
73+
return "..."
74+
}
75+
return "..."
76+
}
5177
}
5278
```
5379

@@ -100,21 +126,23 @@ func pancake(with syrup: Syrup) -> Pancake {
100126
}
101127
```
102128

103-
* **1.9** We follow Xcode's recommended indentation style (i.e. your code should not change if CTRL-I is pressed). When declaring a function that spans multiple lines, prefer using that syntax to which Xcode, as of version 7.3, defaults.
129+
* **1.9** We follow Xcode's recommended indentation style (i.e. your code should not change if CTRL-I is pressed). For multi-line function declarations, use the syntax that [swift-format](https://github.com/swiftlang/swift-format) defaults to in Xcode 16.0 or later.
104130

105131
```swift
106-
// Xcode indentation for a function declaration that spans multiple lines
107-
func myFunctionWithManyParameters(parameterOne: String,
108-
parameterTwo: String,
109-
parameterThree: String) {
132+
// Swift-format indentation for a function declaration that spans multiple lines
133+
func myFunctionWithManyParameters(
134+
parameterOne: String,
135+
parameterTwo: String,
136+
parameterThree: String
137+
) {
110138
// Xcode indents to here for this kind of statement
111139
print("\(parameterOne) \(parameterTwo) \(parameterThree)")
112140
}
113141

114-
// Xcode indentation for a multi-line `if` statement
142+
// Swift-format indentation for a multi-line `if` statement
115143
if myFirstValue > (mySecondValue + myThirdValue)
116-
&& myFourthValue == .someEnumValue {
117-
144+
&& myFourthValue == .someEnumValue
145+
{
118146
// Xcode indents to here for this kind of statement
119147
print("Hello, World!")
120148
}
@@ -136,11 +164,11 @@ someFunctionWithABunchOfArguments(
136164
someStringArgument: "hello I am a string",
137165
someArrayArgument: [
138166
"dadada daaaa daaaa dadada daaaa daaaa dadada daaaa daaaa",
139-
"string one is crazy - what is it thinking?"
167+
"string one is crazy - what is it thinking?",
140168
],
141169
someDictionaryArgument: [
142170
"dictionary key 1": "some value 1, but also some more text here",
143-
"dictionary key 2": "some value 2"
171+
"dictionary key 2": "some value 2",
144172
],
145173
someClosure: { parameter1 in
146174
print(parameter1)
@@ -161,7 +189,8 @@ if firstCondition && secondCondition && thirdCondition {
161189
// NOT PREFERRED
162190
if x == firstReallyReallyLongPredicateFunction()
163191
&& y == secondReallyReallyLongPredicateFunction()
164-
&& z == thirdReallyReallyLongPredicateFunction() {
192+
&& z == thirdReallyReallyLongPredicateFunction()
193+
{
165194
// do something
166195
}
167196
```
@@ -927,7 +956,8 @@ if let woodchuck = woodchuck, canChuckWood(woodchuck) {
927956
// combined because we just return
928957
guard let thingOne = thingOne,
929958
let thingTwo = thingTwo,
930-
let thingThree = thingThree else {
959+
let thingThree = thingThree
960+
else {
931961
return
932962
}
933963

0 commit comments

Comments
 (0)