Stack overflow when defining subscript on CKRecord in Swift

前端 未结 3 1142
有刺的猬
有刺的猬 2021-01-18 12:22

This question asks whether one can use subscripting with CKRecord in Swift. While I already knew how to do what the questioner wanted, every permutation of it g

3条回答
  •  离开以前
    2021-01-18 13:16

    Here’s a simple extension to CKRecord to make it easier to subscript with.

    extension CKRecord {
        struct Sub {
            let record: CKRecord
    
            subscript(key: String) -> CKRecordValue? {
                get {
                    return record.objectForKey(key) as? CKRecordValue
                }
                set {
                    record.setObject(newValue, forKey: key)
                }
            }
        }
    
        var sub: Sub {
            return Sub(record: self)
        }
    
        var 

提交回复
热议问题