hide property in object

I would like to conceal a property within an object. How do I do that?


Please forgive my lack of clarity. I have to use words that doesn't give me the message that I have invalid characters.

Answered by QuinceyMorris in 140211022

There is no Swift access control meaning "private to class", as there is in other languages. Currently (Swift 2.2) "private" means "private to file", as you discovered. You can make it completely private by putting the entire class in a file by itself.


In Swift 3, coming soon, there will be another access control, "private to declaration scope", which is closer to what you expect. Note, though, that there seems to be no plan to implement "private to class" in Swift. Swift access controls are related to a lexical scope, which means a class and its extensions are different scopes. Files and modules are also regarded as scopes, for access control purposes.

If I understand your question : just declare it private:


private myProperty : Atype


It will be visible and accessible from inside the class but not outside

That's what I read in a book. But Language Guide seems to contradict that. According to this page, https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AccessControl.html#//apple_ref/doc/uid/TP40014097-CH41-ID3, declaring the property private would only allow access to the property from the "defining source file". Here is the exact text which I copy and paste:


Private access restricts the use of an entity to its own defining source file. Use private access to hide the implementation details of a specific piece of functionality.


It's found in the section titled Access Levels near the top of the page.


Are you sure about what you are telling me?

I also tested that theory in code. I can still access a property declared as private from outside the object. Here is my test code which I put inside of a test class which conforms to the XCTestCase protocol. (Or it may be inheriting from the XCTestCase class. I can't tell.)


func testAccessLevel() {

class Dog {

private var name = "[Unnamed]"

private func bark() {

print("Woof!")

}

}

class Trainer {

var dog = Dog()

func tellDogToBark() {

dog.bark()

}

}

var dog1 = Dog()

print(dog1.name)

dog1.bark()

var trainer = Trainer()

trainer.tellDogToBark()

}


As you can see, the private property name on the instance of the Dog class can still be accessed from outside the instance.

Accepted Answer

There is no Swift access control meaning "private to class", as there is in other languages. Currently (Swift 2.2) "private" means "private to file", as you discovered. You can make it completely private by putting the entire class in a file by itself.


In Swift 3, coming soon, there will be another access control, "private to declaration scope", which is closer to what you expect. Note, though, that there seems to be no plan to implement "private to class" in Swift. Swift access controls are related to a lexical scope, which means a class and its extensions are different scopes. Files and modules are also regarded as scopes, for access control purposes.

FYI, the Swift 3 proposal is here.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
hide property in object
 
 
Q