zoukankan      html  css  js  c++  java
  • iOS-NSData与NSDictionary的互相转换

    这里,将字典转换成NSData类型。有一点必须注意的,必须实现

    - (id)initWithCoder:(NSCoder *)aDecoder方法和

    - (void)encodeWithCoder:(NSCoder *)aCoder方法。

     

     

    + (NSData *)returnDataWithDictionary:(NSDictionary*)dict

    {

        NSMutableData* data = [[NSMutableData alloc]init];

        NSKeyedArchiver* archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];

        [archiver encodeObject:dict forKey:@"talkData"];

        [archiver finishEncoding];

        return data;

    }

    将字典路径的字符串转换为字典 

    + (NSDictionary *)returnDictionaryWithDataPath:(NSString*)path

    {

        NSData* data = [[NSMutableData alloc]initWithContentsOfFile:path];

        NSKeyedUnarchiver* unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data];

        NSDictionary* myDictionary = [unarchiver decodeObjectForKey:@"talkData"];

        [unarchiver finishDecoding];

       

        return myDictionary;

    }

  • 相关阅读:
    解题报告:luogu P1156
    解题报告:AT3605
    矩阵乘法与斐波那契数列
    九、模块
    八、异常
    七、文件处理
    六、对象和内存分析
    五、函数和内存分析
    四、控制语句
    三、序列
  • 原文地址:https://www.cnblogs.com/XHShare/p/4973193.html
Copyright © 2011-2022 走看看