码迷,mamicode.com
首页 > 其他好文 > 详细

OC中plist文件的读取和写入

时间:2015-04-23 07:04:09      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:

plist文件读取,字典数组转模型数组,即:字典转模型

- (instancetype)initWithDict:(NSDictionary *)dict
{
    self = [super init];
    if (self) {
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}
+ (instancetype)heroWithDict:(NSDictionary *)dict
{
    return [[self alloc] initWithDict:dict];
}
+ (NSArray *)heros
{
    NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"heros.plist" ofType:nil]];
    NSMutableArray *arrayM = [NSMutableArray array];
    for (NSDictionary *dict in array) {
        [arrayM addObject:[self heroWithDict:dict]];
    }
    return arrayM;
}

 plist文件写入,模型数组转字典数组

        NSString *path = [[NSBundle mainBundle] pathForResource:@"heros" ofType:@"plist"];
        BOOL success = YES;
        // 归档写入方式,写入的内容不仅仅包括字典数组,上层还有系统针对对象添加的内容,不可取
        // success = [NSKeyedArchiver archiveRootObject:self.heros toFile:path];
        NSMutableArray *arrayM = [NSMutableArray array];
        for (LPKHeros *model in self.heros) {
            NSDictionary *dict = [model dictionaryWithValuesForKeys:@[@"name", @"icon", @"intro"]];
            [arrayM addObject:dict];
        }
        success = [arrayM writeToFile:path atomically:YES];
        if (success) {
            NSLog(@"写入成功!");
        }

 

OC中plist文件的读取和写入

标签:

原文地址:http://www.cnblogs.com/newbie28/p/4449187.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!