Swift: What does this error: 'private(set)' modifier cannot be applied to read-only properties mean?

和自甴很熟 提交于 2020-05-29 05:00:39

问题


I am bit confused if we can create computed property which is read-only Somethig like:

extension ToMyClass {
    private(set) var isEmpty: Bool {
        return head == nil
    }
} 

While trying to create I got following error:

 error: 'private(set)' modifier cannot be applied to read-only properties

回答1:


You are trying to set a modfier for a computed property, which is always read-only

The code below was taken from: The Swift Programming Language (Swift 4)

struct TrackedString {
    private(set) var numberOfEdits = 0
    var value: String = "" {
        didSet {
            numberOfEdits += 1
        }
    }
}

It should be a stored property



来源:https://stackoverflow.com/questions/45660160/swift-what-does-this-error-privateset-modifier-cannot-be-applied-to-read-o

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