Skip to content

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: .white is better than UIColor.white.
  • Use of self is contentious.

Async Code Conventions

Completion blocks on-main

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!
    }
  }
}

Use of Deferred

// fill in

Clone this wiki locally