Why can't I use subscripting on a CKRecord object in swift?

后端 未结 1 529
既然无缘
既然无缘 2021-01-13 12:39

This has bothered me for a little while. Is there a reason why I need to do this to set an object on a CKRecord.

task.record?.setObject(task.reference, forKe         


        
相关标签:
1条回答
  • 2021-01-13 13:14

    That subscripting is only available from Objective-C, since it's implemented using objectForKeyedSubscript: and setObject:forKeyedSubscript:. Happily, it's easy to extend CKRecord to allow Swift subscripting:

    extension CKRecord {
        subscript(key: String) -> AnyObject! {
            get {
                return self.objectForKey(key)
            }
            set(newValue) {
                self.setObject(newValue as CKRecordValue, forKey: key)
            }
        }
    }
    

    NSHipster has a post on Objective-C subscripting if you want to know more. I'm surprised Swift doesn't bridge that automatically.

    0 讨论(0)
提交回复
热议问题