gpt4 book ai didi

swift - CloudKit - CKRecord.ID 可接受的记录名称?

转载 作者:搜寻专家 更新时间:2023-11-01 06:54:06 25 4
gpt4 key购买 nike

我已经编写了一种在自定义区域中保存记录的方法,它似乎按预期工作。但是,我不确定的一件事是 CKRecord.ID recordName。现在我只使用 UUID 字符串。是否有分配 recordName 的首选方法?当前的 CloudKit 示例非常稀少,而且 CK 文档的相当一部分似乎已经过时。谢谢。

func saveToCloud(record: String, recordType: String, recordTypeField: String, reference: CKRecord?, referenceType: String?) {

let zoneID = CKRecordZone.ID(zoneName: Zone.test, ownerName: CKRecordZone.ID.default.ownerName)
let recordID = CKRecord.ID(recordName: UUID().uuidString, zoneID: zoneID)
let newRecord = CKRecord(recordType: recordType, recordID: recordID)

if let reference = reference, let referenceType = referenceType {
let newReference = CKRecord.Reference(record: reference, action: .none)
newRecord[referenceType] = newReference
}
newRecord[recordTypeField] = record
database.save(newRecord) { (_,error) in
if let err = error as? CKError {
print("ERROR =" , err.userInfo )
}
}
}

最佳答案

我同意 UUID 是理想的。示例:

let newId = CKRecord.ID(recordName: UUID().uuidString)

但是,重要的是不要尝试使用无效字符或零长度字符串创建一个。结果是 fatal error !因此,当从不受信任的来源加载它们时(比如从服务器输出解析),我使用如下安全功能:

extension CKRecord.ID {     
public static func fromUntrusted(_ string: String?) -> CKRecord.ID? {
guard let string = string else { return nil }
guard let _ = string.data(using: .ascii, allowLossyConversion: false) else { return nil }
guard string.count > 0 && string.count < 255 else { return nil }
return CKRecord.ID(recordName: string)
}
}

用法:

if let recordId = CKRecord.ID.fromUntrusted(response) {
// use value
}

关于swift - CloudKit - CKRecord.ID 可接受的记录名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54920478/

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