Indenting Swift code

Issue #93

Hi, here is how I indent my code. Let me know what you think 😉

Using 2 spaces indentation

When possible, configure your editor to use 2 spaces for tab size. You will love it ❤️

spaces

Move first parameter to new line

If there are many parameters, move the first parameter to a new line, and align the other parameters. Remember that the last parenthesis ) should align to the function call

1
2
3
4
5
6
7
let event = NSAppleEventDescriptor(
eventClass: UInt32(kASAppleScriptSuite),
eventID: UInt32(kASSubroutineEvent),
targetDescriptor: target,
returnID: Int16(kAutoGenerateReturnID),
transactionID: Int32(kAnyTransactionID)
)

You can do the same for function declaration

1
2
3
4
5
6
func collectionView(
_ collectionView: UICollectionView,
viewForSupplementaryElementOfKind kind: String,
at indexPath: IndexPath) -> UICollectionReusableView {
// your code goes here
}

Shouldn’t use trailing closure if there are more than 2 closures

Here is how to use UIView.animate

1
2
3
4
5
6
7
8
9
10
11
12
13
UIView.animate(
withDuration: 5,
delay: 5,
usingSpringWithDamping: 1,
initialSpringVelocity: 1,
options: .curveEaseIn,
animations: {
self.tableView.alpha = 1
},
completion: { _ in
self.view.isHidden = true
}
)

Here is how to use RxSwift subscribe

1
2
3
4
5
6
7
8
9
10
11
12
13
14
friendsObservable.subscribe(
onNext: { friends in

},
onError: { error in

},
onCompleted: {

},
onDisposed: {

}
)

Shouldn’t use trailing closure when chaining methods

Also, the next method call should start on same line

1
2
3
4
5
6
7
8
let items = [1, 2, 3, 4, 5]
let results = items.map({
return String($0)
}).flatMap({
return Int($0)
}).filter({
return $0 > 2
}).sorted()

Comments