NSUserDefaults和归档检索问题

时间:2015-05-13 11:35:35

标签: ios objective-c nsarray nsuserdefaults nskeyedunarchiver

我遇到// Dump the load addresses of AppName and libXxx.dylib to help tools/process_bug_reports.py uint32_t numImages = _dyld_image_count(); for (uint32_t i = 0; i < numImages; i++) { const struct mach_header *header = _dyld_get_image_header(i); const char *name = _dyld_get_image_name(i); const char *p = strrchr(name, '/'); if (p && (strcmp(p + 1, "AppName") == 0 || strcmp(p + 1, "libXxx.dylib") == 0)) { loginf(@"module=%s, address=%p", p + 1, header); } } 的奇怪问题。该应用程序需要在具有不同输入的两种情况下使用相同的Web服务。一个处于初始状态,其中来自服务器的响应在NSArray的帮助下保存到NSUserDefaults。接下来是在完成特定操作时,然后根据响应修改NSKeyedArchiver中存储的值。

NSUserDefaults

当我尝试取消归档之前存储的数据时,会打印 @“6个对象”而不是6个ActiveOrder对象。

NSArray *sorted = [ActiveOrder arrayOfModelsFromDictionaries: [responseObject objectForKey:@"GetActiveOrdersResult"]]; //ActiveOrder is a custom JSON model object
NSSortDescriptor *orderCreateDate = [[NSSortDescriptor alloc] initWithKey:@"orderCreateDate" ascending:YES];
ordersList = [[NSMutableArray alloc] initWithArray: [sorted sortedArrayUsingDescriptors: [NSArray arrayWithObjects:orderCreateDate, nil]]];

NSMutableArray *archiveArray = [NSMutableArray arrayWithCapacity:ordersList.count];
for (ActiveOrder *itemDetail in ordersList) {
    [archiveArray addObject:itemDetail];
} //if i log 'archiveArray' it prints valid value (array of 'ActiveOrder' objects)

NSData *itemDetailObject = [NSKeyedArchiver archivedDataWithRootObject:archiveArray];
[[NSUserDefaults standardUserDefaults] setObject:itemDetailObject
                 forKey:TAG_ACTIVE_ORDERS_RESPONSE];

我正在使用上面的行来访问存储的值以进行修改。我很困惑为什么要打印@“6个对象”而不是实际的对象。 它随机崩溃(猜测这可能是原因)。有NSMutableArray *itemsFromLastResult = [[NSKeyedUnarchiver unarchiveObjectWithData: [[NSUserDefaults standardUserDefaults] objectForKey: TAG_ACTIVE_ORDERS_RESPONSE]] mutableCopy]; / NSKeyedArchiver

我做错了吗?

了解解决方案会很棒。谢谢!

1 个答案:

答案 0 :(得分:0)

NSUserDefaults异步保存对象! 尝试 [[NSUserDefaults standardUserDefaults] synchronize]; 在setObjectForKey之后