Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naming convention for Private Properties

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?

like image 994
ad-johnson Avatar asked Aug 05 '17 12:08

ad-johnson


Video Answer


1 Answers

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.

like image 110
Idan Avatar answered Oct 18 '22 04:10

Idan