raywenderlich.com 公式Swiftスタイルガイドを読みました




Copyright (c) 2016-2017 Razeware LLC

Use classes for things that do have an identity or a specific life cycle. You would model a person as a class because two person objects are two different things. Just because two people have the same name and birthdate, doesn't mean they are the same person. But the person's birthdate would be a struct because a date of 3 March 1950 is the same as any other date object for 3 March 1950. The date itself doesn't have an identity.


For conciseness, avoid using self since Swift does not require it to access an object's properties or invoke its methods. Use self only when required by the compiler (in @escaping closures, or in initializers to disambiguate properties from arguments). In other words, if it compiles without self then omit it.

簡潔な表現のために,selfを使うのは避けましょう.Swiftはオブジェクトのプロパティやメソッドの呼び出しのためにselfを必要としません. コンパイラエラーが発生する場合のみselfを使うようにしましょう(@escapingクロージャやイニシャライザにおいて,引数とプロパティが曖昧な場合にそうなります.)言い方を変えると,selfを付けず にコンパイラエラーが出なければ,selfを省略してください.

Unused (dead) code, including Xcode template code and placeholder comments should be removed. An exception is when your tutorial or book instructs the user to use the commented code.


For functions with long signatures, add line breaks at appropriate points and add an extra indent on subsequent lines:

func reticulateSplines(spline: [Double], adjustmentFactor: Double,
    translateConstant: Int, comment: String) -> Bool {
  // reticulate code goes here


Use trailing closure syntax only if there's a single closure expression parameter at the end of the argument list. Give the closure parameters descriptive names.


Note: The advantage of using a case-less enumeration is that it can't accidentally be instantiated and works as a pure namespace.

注意: case無しの列挙型は間違ってインスタンス化される心配がないため,純粋なネームスペースとして働きます.