【问题标题】:How can I convert NSDictionary to NSData and vice versa?如何将 NSDictionary 转换为 NSData,反之亦然?
【发布时间】:2011-07-27 15:38:10
【问题描述】:

我正在使用蓝牙发送NSStringUIImage。我决定将两者都存储在NSDictionary 中,然后将字典转换为NSData

我的问题是如何将NSDictionary 转换为NSData,反之亦然?

【问题讨论】:

    标签: iphone objective-c ios nsdictionary


    【解决方案1】:

    NSDictionary -> NSData:

    NSData *myData = [NSKeyedArchiver archivedDataWithRootObject:myDictionary];
    

    NSData -> NSDictionary:

    NSDictionary *myDictionary = (NSDictionary*) [NSKeyedUnarchiver unarchiveObjectWithData:myData];
    

    【讨论】:

    • 对于 NSData -> NSDictionary,强制转换为 NSDictionary 似乎是不必要的。
    【解决方案2】:

    NSDictionary -> NSData:

    NSMutableData *data = [[NSMutableData alloc] init];
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    [archiver encodeObject:yourDictionary forKey:@"Some Key Value"];
    [archiver finishEncoding];
    [archiver release];
    
    // Here, data holds the serialized version of your dictionary
    // do what you need to do with it before you:
    [data release];
    

    NSData -> NSDictionary

    NSData *data = [[NSMutableData alloc] initWithContentsOfFile:[self dataFilePath]];
    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
    NSDictionary *myDictionary = [[unarchiver decodeObjectForKey:@"Some Key Value"] retain];
    [unarchiver finishDecoding];
    [unarchiver release];
    [data release];
    

    您可以使用任何符合 NSCoding 的类来做到这一点。

    source

    【讨论】:

    • 不要从这个答案中拿走任何东西,而只是说明它不符合 ARC
    • 是的,我的回答来自 2011 年,那时 ARC 不存在
    【解决方案3】:

    使用 NSJSON 序列化:

    NSDictionary *dict;
    NSData *dataFromDict = [NSJSONSerialization dataWithJSONObject:dict
                                                           options:NSJSONWritingPrettyPrinted
                                                             error:&error];
    
    NSDictionary *dictFromData = [NSJSONSerialization JSONObjectWithData:dataFromDict
                                                                 options:NSJSONReadingAllowFragments
                                                                   error:&error];
    

    最新的返回id,所以最好在你转换后检查返回的对象类型(这里我转换为NSDictionary)。

    【讨论】:

      【解决方案4】:

      Swift 2 中,您可以这样做:

      var dictionary: NSDictionary = ...
      
      /* NSDictionary to NSData */
      let data = NSKeyedArchiver.archivedDataWithRootObject(dictionary)
      
      /* NSData to NSDictionary */
      let unarchivedDictionary = NSKeyedUnarchiver.unarchiveObjectWithData(data!) as! NSDictionary
      

      Swift 3 中:

      /* NSDictionary to NSData */
      let data = NSKeyedArchiver.archivedData(withRootObject: dictionary)
      
      /* NSData to NSDictionary */
      let unarchivedDictionary = NSKeyedUnarchiver.unarchiveObject(with: data)
      

      【讨论】:

      • 没用! Swift 3 等效代码不起作用。 let data = NSKeyedArchiver.archivedData(withRootObject: dataFromNetwork) [_SwiftValue encodeWithCoder:]: unrecognized selector sent to instance ...
      • 感谢您的快速响应。我的意思是——Swift 3 代码也不起作用。我的数据来自 3lvis/networking HTTP 库,它是Any? 类型。但它显示从NSDictionary 转换为NSData 时出错。所以我尝试了你的代码。它没有用。
      【解决方案5】:

      来自 NSData 的 NSDictionary

      http://www.cocoanetics.com/2009/09/nsdictionary-from-nsdata/

      NSDictionary 到 NSData

      您可以为此使用 NSPropertyListSerialization 类。看看它的方法:

      + (NSData *)dataFromPropertyList:(id)plist format:(NSPropertyListFormat)format
                                    errorDescription:(NSString **)errorString
      

      返回一个包含指定格式的给定属性列表的 NSData 对象。

      【讨论】:

      • 对于大多数 Mac OS 开发人员来说,这是一个非常优雅的解决方案。
      【解决方案6】:

      请试试这个

      NSError *error;
      NSDictionary *responseJson = [NSJSONSerialization JSONObjectWithData:webData
                                      options:NSJSONReadingMutableContainers
                                      error:&error];
      

      【讨论】:

      • 这是response,而不是responce。我为你更正了。
      猜你喜欢
      • 1970-01-01
      • 2011-10-08
      • 1970-01-01
      • 2020-02-19
      • 2015-01-03
      • 1970-01-01
      • 2022-01-09
      • 2021-06-10
      • 2012-03-24
      相关资源
      最近更新 更多