简体   繁体   English

使用CloudKit从Swift中的CKRecord中提取NSArray

[英]Extracting an NSArray from a CKRecord in Swift with CloudKit

How can" I fill an NSArray (an array of strings) from a CKRecord? 如何“我从CKRecord填充NSArray(字符串数组)?

I created the record successfully with: myPrivateDatabase.saveRecord 我使用:myPrivateDatabase.saveRecord成功创建了记录

and I can fetch the record successfully with: myPrivateDatabase.fetchRecordWithID 我可以使用:myPrivateDatabase.fetchRecordWithID成功获取记录

I can see that the data is in the record with: println(returnRecord.objectForKey("key1")) in the form of comma delimited, quoted strings 我可以看到数据在记录中:println(returnRecord.objectForKey(“key1”))以逗号分隔的形式,引用的字符串

But I can't figure out how to get that back into the NSArray 但我无法弄清楚如何将其重新纳入NSArray

Help! 救命!

Thanks :-) 谢谢 :-)

If you just put the array in a string field, then you have to parse it back. 如果您只是将数组放在字符串字段中,那么您必须将其解析回来。

let line = returnRecord.objectForKey("key1")
let values = line.componentsSeparatedByCharactersInSet(returnRecord.objectForKey("key1"))

then just loop through the values and remove the leading and trailing quotes. 然后只需遍历值并删除前导和尾随引号。 You do have to be aware that it will go wrong when you have strings that contains a comma. 你必须要知道,当你有包含逗号的字符串时会出错。 To solve that issue you should use NSScanner for parsing the line 要解决该问题,您应该使用NSScanner来解析该行

Did you specify the field in CloudKit as a 'String List' or is it a String? 您是否将CloudKit中的字段指定为“字符串列表”,还是字符串?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM