简体   繁体   English

在特定条件下崩溃unarchiveObjectWithFile

[英]Crash in unarchiveObjectWithFile under specific conditions

I have the following code to read an archived array: 我有以下代码来读取存档数组:

id temp = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
if(temp && [temp isKindOfClass:[NSArray class]])
{
   self.posts      = [temp mutableCopy];
}

the following code is to save the array 以下代码是保存数组

-(void)savePostList
{
    NSURL *tempURL = [[[ZZAppDelegate sharedAppDelegate] applicationCacheDirectory] URLByAppendingPathComponent:@"postCache.plist"];
    [NSKeyedArchiver archiveRootObject:self.posts 
                                toFile:[tempURL path]];
}

Generally everything works perfectly, however in very particular (currently undetermined) circumstances the app starts crashing at startup with an exception: 通常情况下一切都很完美,但是在非常特殊(当前未确定)的情况下,应用程序在启动时会因异常而崩溃:

-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x275900

the crash actually happens inside of the call to: 崩溃实际上是在调用内部发生的:

id temp = [NSKeyedUnarchiver unarchiveObjectWithFile:path];

Having pulled out the plist from the device it looks very odd (and very empty): 从设备中拔出钳子后,它看起来非常奇怪(而且非常空):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array/>
</plist>

If I wrap the whole thing in a @try / @catch clause then 'everything is fine' as so: 如果我将整个事物包装在@try / @catch子句中,那么“一切都很好”,如下所示:

        @try {
            id temp = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
            if(temp && [temp isKindOfClass:[NSArray class]])
            {
                self.posts      = [temp mutableCopy];
                if(self.posts)
                {
                    [self precacheImages];
                }
            }
        }
        @catch (NSException *exception) {
            // exception thrown - delete file
            NSLog(@"An exception occurred reading file - deleting");
            NSError * err;

            if(![[NSFileManager defaultManager] removeItemAtPath:path error:&err])
            {
                NSLog(@"Error deleting file: %@", err);
            }
        }

My question is: does anyone have any idea what would cause this (as in the .plist is 'empty') and then for the unarchiveObjectWithFile call to cause an exception the way it does (which according to a stack trace is inside of the system library). 我的问题是:有没有人知道是什么会导致这种情况(如.plist中的'空')然后对unarchiveObjectWithFile调用引起异常(根据堆栈跟踪在系统内部)图书馆)。

你会想要更像这样的东西来反序列化plist

NSArray * someArray = [NSPropertyListSerialization dataFromPropertyList: [NSData dataWithContentsOfFile:path] format:NSPropertyListXMLFormat_v1_0 errorDescription:nil];

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

相关问题 如何使用[NSKeyedUnarchiver unarchiveObjectWithFile]防止损坏的文件崩溃? - How to prevent crash on corrupt file with [NSKeyedUnarchiver unarchiveObjectWithFile]? unarchiveObjectWithFile保留/自动释放需要吗? - unarchiveObjectWithFile retain / autorelease needed? iPhone在特定设备上崩溃 - iphone crash on specific device 在某些情况下如何在UITableView中显示消息 - How to display message in UITableView under certain conditions ARC在什么条件下保留这个对象? - Under what conditions will this object be retained by ARC? iOS:日期比较在实际条件下失败,在模拟条件下可以工作 - iOS: Date comparison failing under real-world conditions, works under simulated conditions 调试 iOS 应用程序:在 Xcode 下崩溃但在 iPhone 上继续工作 - Debugging iOS application: crash under Xcode but keep working on iPhone 填充表格视图 - 如何在某些条件下不返回任何单元格 - Populating a Table View - How to Return No Cell Under Certain Conditions 在什么条件下,instancesRespondToSelector:返回true,但是performSelector:抛出异常 - Under what conditions might instancesRespondToSelector: return true, but performSelector: throw an exception 在什么条件下[NSEvent字符]可以是长度大于1的NSString? - Under what conditions can [NSEvent characters] be a NSString of length greater than 1?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM