Naming convention for Private Properties

北战南征 提交于 2019-12-13 15:23:20

问题


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:


In this Swift Guide style, you can find a similar reference:

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

In The swift Programming Language book (swift 4 beta) (link to ), you can also find these examples:

page 812:

private var privateInstance = somePrivateClass()

page 822:

private var privateVariable = 12

Excerpt From: Apple Inc. “The Swift Programming Language (Swift 4 beta).” iBooks.

I do believe that the second option is the correct way to go.



来源:https://stackoverflow.com/questions/45521678/naming-convention-for-private-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!