【发布时间】:2014-05-13 01:31:33
【问题描述】:
我正试图将注意力集中在 iCloud 存储上。我已经阅读了iCloud Design Guide 和一些关于 SO 的问题。这是我的场景:
我有一个只有NSMutableArray 的类...该集合包含我的自定义对象,这些对象都遵守NSCoding,并且在本地保存非常有效。该课程是 MasterList 并且具有属性 masterList,非常棒 - 我知道 :-p。当我研究如何开始实施 iCloud 时,我认为 KVS 会很棒,因为我的数据占用空间非常小。当我尝试这样做时,我不断得到:
[NSUbiquitousKeyValueStore setObject:forKey:]: Attempt to insert non-property value <my custom objects>...
在了解原因之后,您似乎只能使用 KVS 存储标量和 P-List 类型。所以我转而使用UIDocument 并为此苦苦挣扎。
底线 -
对于自定义对象,是一定要用UIDocument,还是可以用KVS?
和
如果我必须使用UIDocument,你们中是否有人读过本质上很简单的教程(可能存储了一些道具并将它们加载回来,可能在一个示例项目中?)让您点击了吗?
如果有帮助的话,下面是我使用 KVS 的代码。不是生产或任何东西,只是想让它工作:
//Load it
+(MasterList *)decodeMasterList:(MasterList *)objectToDecode
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSURL *dataFile = [self pathForDocumentsFile:kFilePathAllLists];
NSString *filePath = [dataFile path];
objectToDecode = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
//First time runnign the app :-)
if (!objectToDecode) objectToDecode = [[MasterList alloc] init];
if ([defaults boolForKey:kIsUsingiCloud])
{
objectToDecode.masterList = [[[NSUbiquitousKeyValueStore defaultStore] arrayForKey:kiCloudPath] mutableCopy];
//If we just started and nothing is there
if (!objectToDecode.masterList)
{
objectToDecode.masterList = [[NSMutableArray alloc] initWithObjects:[CustomList new], nil];
}
}
return objectToDecode;
}
//Save it
+(BOOL)encodeMasterList:(MasterList *)objectToEncode
{
@try
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults boolForKey:kIsUsingiCloud])
{
[[NSUbiquitousKeyValueStore defaultStore] setArray:objectToEncode.masterList forKey:kiCloudPath];
[[NSUbiquitousKeyValueStore defaultStore] synchronize];
}
NSURL *dataFile = [FileSystemHelper pathForDocumentsFile:kFilePathAllLists];
NSString *filePath = [dataFile path];
[NSKeyedArchiver archiveRootObject:objectToEncode toFile:filePath];
}
@catch (NSException *exception)
{
return NO;
}
return YES;
}
【问题讨论】:
标签: ios objective-c icloud uidocument