This property can be read, but cannot be set from the outside.
You assign a lower access level by writing private(set)
.
Its works like Public getter and Private setter.
//MARK:- Foo
class Foo {
private var name: String
private var ID: String
//initialize
init(name:String, ID:String){
self.name = name
self.ID = ID
}
}
We will get an error when to try to access private
variable.
But we can fix this error in a single line by changing private
access level to private(set)
.
//MARK:- Foo
class Foo {
private(set) var name: String
private var ID: String
//initialize
init(name:String, ID:String) {
self.name = name
self.ID = ID
}
}
So you can easy access the private variable, constant, property, or subscript.
//Access class value from Foo
let fooObjc = Foo.init(name: "test", ID: "9900")
print(fooObjc.name)
Use fileprivate(set)
, private(set)
, and internal(set)
to change the access level of this synthesized setter in exactly the same way as for an explicit setter in a computed property.
This property can be read, but cannot be set from the outside.
Ref: Click the link to know more