我班上有这样的声明:

var peerDevice: [String: Any]?

private var peerDeviceSettings:[String:Any]? {
    get {
        if let settings = peerDevice?["settings"] as? [String:Any] {
            return settings
        }

        return nil
    }
}

现在的问题是,如果我想更改peerDeviceSettings,例如:
 fileprivate func set(_ value: Any?, forKey key: Key) {
    if peerDeviceSettings != nil {
        peerDeviceSettings?[key.rawValue] = value
    }
 }

我有个错误
 Cannot assign through subscript: 'peerDeviceSettings' is a get-only property

关键是如何在不创建新的可变变量peerDeviceSettings的情况下重新安排这件事?

最佳答案

只需将set (setter)添加到属性并在那里处理它。

private var peerDeviceSettings:[String:Any]? {
    get {
        if let settings = peerDevice?["settings"] as? [String:Any] {
            return settings
        }

        return nil
    }
    set {

    }
}

关于ios - Swift无法通过下标分配:“peerDeviceSettings”是仅获取属性”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57198443/

10-09 02:35