美文网首页
在plist中存储字典

在plist中存储字典

作者: 小码农杰哥 | 来源:发表于2017-07-11 20:37 被阅读0次

    写入字典,然后取出来。plist里面的可以进行替换

    +(void)writDataWithDic:(NSDictionary*)dic{

    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

    NSString *filename = [path stringByAppendingPathComponent:@"china.plist"];

    NSFileManager *manager = [NSFileManager defaultManager];

    [manager createFileAtPath:path contents:nil attributes:nil];

    [dic writeToFile:filename atomically:YES];

    }

    +(NSDictionary*)readDataWithKey:(NSString *)key{

    NSString *pathp = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

    NSString *filenamep = [pathp stringByAppendingPathComponent:@"china.plist"];

    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:filenamep];

    return dict[key];

    }

    调用

    NSDictionary *dic = @{@"a":@"123",@"b":@"abc"};

    [Tools writDataWithDic:dic];

    NSDictionary *dicT = @{@"a":@"陪我妹电饭锅",@"345":@"电视剧九分裤"};

    [Tools writDataWithDic:dicT];

    NSLog(@"%@----------%@",[Tools readDataWithKey:@"a"],NSHomeDirectory());

    相关文章

      网友评论

          本文标题:在plist中存储字典

          本文链接:https://www.haomeiwen.com/subject/yyamhxtx.html