【问题标题】:iPhone: Missing method? NSDictionary dictionaryWithData:iPhone:缺少方法? NSDictionary 字典WithData:
【发布时间】:2010-07-10 16:06:14
【问题描述】:

Web 服务以 plist 的形式提供数据。下载后,我拥有一个 NSData 对象中的所有数据,我想将其转换为 NSDictionary。现在我所知道的唯一方法是不手动解析它(糟糕)是这样的:

static NSString *fileName = @"tempFile";
[data writeToFile: fileName atomically: NO];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile: fileName];

我很惊讶 NSDictionary 没有 dictionaryWithData: 方法。如果 dictionaryWithString: 存在,我什至不介意先将 NSData 转换为 NSString。很明显,SDK 在幕后做了类似的事情,但它并没有暴露给开发人员。

谁能提出一个更好的方法来做到这一点?我需要转换的文件之一可能非常大,执行写入文件+读取文件的操作可能会有点慢。

【问题讨论】:

    标签: iphone nsdictionary


    【解决方案1】:

    使用以下方法之一:

    +[NSPropertyListSerialization propertyListFromData:mutabilityOption:format:errorDescription:]
    +[NSPropertyListSerialization propertyListWithData:options:format:error:]
    

    前者在 iOS 2.0+ 中可用,但在 4.0 中已被弃用。

    【讨论】:

    • 太棒了!这正是我所需要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多