问题
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, forKey:ReferenceField)
instead of this
task.record?[ReferenceField] = task.reference
From what I read in the docs CKRecord should be subscript friendly
回答1:
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.
来源:https://stackoverflow.com/questions/27108714/why-cant-i-use-subscripting-on-a-ckrecord-object-in-swift