我想将字典对象保存到CloudKit,因此我将字典存档为NSData对象,并将其保存到名为"字符串"的字节属性中。在我的记录中。当我保存记录并调用println()时,记录返回我想要的所有属性,包括NSData对象。但是,当我使用其recordID获取该记录时,NSData对象就不存在了。不知道发生了什么。
以下是我的保存功能的代码:
let recordIDData = entity.valueForKey("recordID") as! NSData
let recordID = NSKeyedUnarchiver.unarchiveObjectWithData(recordIDData) as! CKRecordID
let data = NSKeyedArchiver.archivedDataWithRootObject(statDictionary)
publicDB.fetchRecordWithID(recordID, completionHandler: {(record, error) -> Void in
if(error != nil){
self.notifyUser("Save Error", message: "The Record Wasn't Saved")
}else{
record.setObject(data, forKey: "stats")
println(record)
}
})
以下是我的视图控制器中提取记录的代码
let data = record!.objectForKey("stats") as? NSData
if(data != nil){
let stats = NSKeyedUnarchiver.unarchiveObjectWithData(data!) as! NSDictionary
notifyUser("Success!", message: "The Dictionary was retrieved!")
println(stats)
}
println(record!)
let opponent = record!.objectForKey("opponent") as! String
重要的是要注意objectForKey(" opponent")调用从记录中返回正确的字符串。
另外,我知道statsDictionary对象有值,因为我也同时将这个字典保存到coreData,当我以这种方式检索数据时它工作正常。即使在CloudKit仪表板中,记录也不会显示stats属性中保存的任何内容。
这是我保存时的控制台日志:
statsDictionary = [section1Digs: 1, section1Assists: 1, section1Hits: 7, section1Blocks: 4, section1Points: 1]
<NSManagedObject: 0x1700af060> (entity: GirlsVolleyball; id: 0xd000000000300002 <x-coredata://21F5B76F-ECDB-400A-8BEB-06D201EC6C1C/GirlsVolleyball/p12> ; data: {
date = "05/10, 03:55 PM";
location = Home;
opponent = Haverhill;
recordID = <62706c69 73743030 d4010203 0405062d 2e582476 65727369 6f6e5824 6f626a65 63747359 24617263 68697665 72542474 6f70>;
stats = <62706c69 73743030 d4010203 0405062a 2b582476 65727369 6f6e5824 6f626a65 63747359 24617263 68697665 72542474 6f70>;
timeStamp = nil;})
<CKRecord: 0x170130680; recordType=Volleyball, recordID=326024E8-A25C-46A7-9C21-AE988D1CBA54:(_defaultZone:__defaultOwner__), recordChangeTag=i9svtxn2, values={
date = "05/10, 03:55 PM";
gender = girls;
location = Home;
opponent = Haverhill;
publishData = No;
stats = <62706c69 73743030 d4010203 0405062a 2b582476 65727369 6f6e5824 6f626a65 63747359 24617263 68697665 72542474 6f701200 0186a0ad 0708191a 1b1c1d1e 1f202122 2355246e 756c6cd3 090a0b0c 1218574e 532e6b65 79735a4e 532e6f62 6a656374 73562463 6c617373 a50d0e0f 10118002 80038004 80058006 a5131415 16178007 80088009 800a800b 800c5c73 65637469 6f6e3144 6967735f 100f7365 6374696f 6e314173 73697374 735c7365 6374696f 6e314869 74735e73 65637469 6f6e3142 6c6f636b 735e7365 6374696f 6e31506f 696e7473 233ff000 00000000 00233ff0 00000000 00002340 1c000000 00000023 40100000 00000000 233ff000 00000000 00d22425 26275a24 636c6173 736e616d 65582463 6c617373 65735c4e 53446963 74696f6e 617279a2 28295c4e 53446963 74696f6e 61727958 4e534f62 6a656374 5f100f4e 534b6579 65644172 63686976 6572d12c 2d54726f 6f748001 00080011 001a0023 002d0032 00370045 004b0052 005a0065 006c0072 00740076 0078007a 007c0082 00840086 0088008a 008c008e 009b00ad 00ba00c9 00d800e1 00ea00f3 00fc0105 010a0115 011e012b 012e013b 01440156 0159015e 00000000 00000201 00000000 0000002e 00000000 00000000 00000000 00000160>;}>
唯一奇怪的是,NSData值在记录上看起来与CoreData实体上的不同。
以下是我稍后检索相同记录时的控制台日志:
<CKRecord: 0x17012cb20; recordType=Volleyball, recordID=326024E8-A25C-46A7-9C21-AE988D1CBA54:(_defaultZone:__defaultOwner__), recordChangeTag=i9svtxn2, values={
date = "05/10, 03:55 PM";
gender = girls;
location = Home;
opponent = Haverhill;
publishData = No;}>
不确定为什么统计数据属性不会显示。
无论如何......帮助
答案 0 :(得分:0)
解决方案:
我只是检索记录但是我没有将其保存回云端,所以我只是在fetchRecordWithID函数的完成块中调用了一个saveRecord函数,它就可以工作了!