-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Swift Style Guides
Garvan Keeley edited this page May 21, 2019
·
14 revisions
Swift code should generally follow the conventions listed at https://github.com/raywenderlich/swift-style-guide.
Some rules we've worked out we all like:
- Selectors:
#selector(ClassName.methodName)is clearer than#selector(methodName). - Type inference:
.whiteis better thanUIColor.white. - Use of
selfis contentious.
Callers expect completion blocks to run on the main thread, this helps to contain off-main thread code such that the caller has no threading considerations/risk. It is rare that the result of a long-running I/O call (such as networking, db access, file access) needs to be handled off main, but if your code does this, please document clearly.
func exampleOfCallingBackOnMainThread(completion: () -> Void) {
DispatchQueue.global().async {
doLotsOfWork()
DispatchQueue.main.async {
completion() // completion is on main, good!
}
}
}
// fill in