04. Set Read-only permission of the property

It will take about 1 minutes to finish reading this article.

In Swift, there is no keyword “readonly” like Objective-C to set read-only access permissions. How can Swift implement read-only access permissions? We can do this as follows:

1
2
3
class MyClass {
private(set) var name: String?
}

External access is normal:

1
2
let a = ClassA()
print(a.title)

But the following is wrong:

1
a.title = "1234"

The following errors will be reported:

1
Cannot assign to property: 'title' setter is inaccessible