gpt4 book ai didi

ios - 如何定义 CKRecord 类

转载 作者:行者123 更新时间:2023-11-28 07:11:53 27 4
gpt4 key购买 nike

我认为将包含信息的基本数据对象定义为 CKRecord 可能是个好主意,因为我显然会使用 CloudKit。但是,我不知道如何创建一个 CKRecord 类。特别是,我不知道如何定义它的“recordType”。

我的具体问题:为什么行 "self.recordType = "myRecordType"会导致错误 "Can not assign to recordType in self"?每个 CKRecord 类都是相同的类型,那么我该如何分配它是一个记录类型?

如果您有观察,我的第二个问题是:您是否发现直接在查询等中使用 CloudKit 中的此类对象(读取和写入)有任何其他问题?如果您在某处看到类似的示例代码,请告诉我。谢谢

class testCKRecordClass: CKRecord {

override init!(recordType: String!) {
self.recordType = "myRecordType"
}

required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

var title: NSString {
get {
return self.objectForKey("title") as NSString
}
set {
self.setObject(newValue, forKey: "title")
}
}

var location: CLLocation? {
get {
return self.objectForKey("location") as? CLLocation
}
set {
self.setObject(newValue, forKey: "location")
}
}

最佳答案

CKRecord 是 CloudKit 方法返回的内容。无法将其解析为您的 testCKRecordClass(很容易)。所以你只能用它来创建一个 CKRecord 来将它写入 CloudKit。

recordType 只能由 CKRecord 的构造函数发起。如果你调用 super.init(recordType) 也许它可以工作

看起来您只想要一种尽可能简单地使用 CloudKit 的方法。如果是这样,那么我也可以建议你看看 https://github.com/evermeer/EVCloudKitDao它是一个 CloudKit 便利库,将使用反射来解析 CKRecord 和解析 CKRecord。

关于ios - 如何定义 CKRecord 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28142731/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com