【问题标题】:Restore object from archived file从归档文件中恢复对象
【发布时间】:2012-03-04 18:21:29
【问题描述】:

我正在使用

[NSKeyedArchiver archiveRootObject:data toFile:file];

将 NSMutableArray 保存到文件中。效果很好。

当我尝试取回数组时,问题就来了。

NSMutableArray *s = [NSKeyedUnarchiver unarchiveObjectWithData:file];

我来了

Incompatible pointer types sending NSString *__strong to parameter of type NSData

这里有什么问题?

【问题讨论】:

    标签: objective-c ios nskeyedarchiver


    【解决方案1】:

    您正在尝试使用unarchiveObjectWithData:,它希望您将文件的内容传递给它。尝试改用unarchiveObjectWithFile:,它希望您将文件名传递给它。

    【讨论】:

      【解决方案2】:
      NSData* arrayData = [NSData dataWithContentsOfFile:file];
      NSMutableArray* s = [NSKeyedUnarchiver unarchiveObjectWithData:arrayData]
      

      对于生产代码,您应该在假设数据有效之前进行一些类型/空值检查。

      【讨论】:

        猜你喜欢
        • 2011-09-26
        • 1970-01-01
        • 2011-01-17
        • 2010-10-28
        • 2014-01-12
        • 1970-01-01
        • 2013-07-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多