美文网首页
IOS plist 文件写入与读取

IOS plist 文件写入与读取

作者: 红姑娘 | 来源:发表于2016-12-22 12:49 被阅读318次

    数组写入plist文件(文件储存到cache路径下)

    NSArray *words = @[@"a", @"b", @"c", @"d", @"e", @"f", @"g", @"h", @"i", @"j", @"k", @"l", @"m", @"n", @"o", @"p", @"q", @"r", @"s", @"t", @"u", @"v", @"w", @"x", @"y", @"z"];
    
    NSString *cachePatch = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
    
    NSString *filePath = [cachePatch stringByAppendingPathComponent:@"words.plist"];
    
    [words writeToFile:filePath atomically:YES];
    

    字典写入plist文件

        NSDictionary *personInfo = @{
                               @"name" : @"amber",
                               @"age" : @"18",
                               @"height" : @"165"};
    
        NSString *cachePatch = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
        NSString *filePath = [cachePatch stringByAppendingPathComponent:@"personInfo.plist"];
        NSURL *fileUrl = [NSURL fileURLWithPath:filePath];
        [personInfo writeToURL:fileUrl atomically:YES];
    

    将字典数组写入plist文件

    NSArray *products = @[
                              @{@"icon" : @"liantiaobao", @"title" : @"链条包"},
                              @{@"icon" : @"shoutibao", @"title" : @"手提包"},
                              @{@"icon" : @"danjianbao", @"title" : @"单肩包"},
                              @{@"icon" : @"shuangjianbao", @"title" : @"双肩包"},
                              @{@"icon" : @"xiekuabao", @"title" : @"斜挎包"},
                              @{@"icon" : @"qianbao", @"title" : @"钱包"}
                              ];
    NSString *cachePatch =  NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
    NSString *filePath = [cachePatch stringByAppendingPathComponent:@"products.plist"];
    NSURL *fileUrl = [NSURL fileURLWithPath:filePath];
    [products writeToURL:fileUrl atomically:YES];
    

    相关文章

      网友评论

          本文标题:IOS plist 文件写入与读取

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