Naming convention for Private Properties

时间:2017-08-05 12:03:35

标签: swift swift3 naming-conventions

I've seen underscore used as a prefix for private properties

private var _aPrivateVar: String = "I am private"

I've seen them not used

private var aPrivateVar: String = "I am private"

Syntactically, it makes no difference and my preference is to not use them. However, what's the accepted convention in Swift so that I pick the right habit up?

1 个答案:

答案 0 :(得分:4)

在此Swift Guide style中,您可以找到类似的参考:

private var centerString: String {
  return "(\(x),\(y))"
}

The swift Programming Language book (swift 4 beta)(链接到)中,您还可以找到以下示例:

第812页:

private var privateInstance = somePrivateClass()

第822页:

private var privateVariable = 12

摘自:Apple Inc.“Swift编程语言(Swift 4 beta)。”iBooks。

我确实认为第二种选择是正确的方法。