ios开发--字典转模型

来源:互联网 发布:网络歌手音频资料 编辑:程序博客网 时间:2024/05/27 20:51

app.plist文件内容如下



1. 先从程序路径下获取该文件内容

创建一个app类

+ (NSArray *)app {    // 获取路径    NSString *path = [[NSBundle mainBundle] pathForResource:@"app" ofType:@"plist"];    // 获取文件内容    NSArray *array = [NSArray arrayWithContentsOfFile:path];        // 模型转换    NSMutableArray *arrayM = [NSMutableArray array];    for (NSDictionary *dict in array) {        [arrayM addObject:[self appWithDict:dict]];    }        return arrayM;}


2. 字典转模型

- (instancetype)initWithDict:(NSDictionary *)dict {    self = [super init];    if (self) {        // KVC        //[self setValuesForKeysWithDictionary:dict];                self.name = dict[@"name"];        self.icon = dict[@"icon"];    }    return self;}

3. 获取app.plist中的内容

-(NSArray *)appList {    if (!_appList) {                _appList = [App app];    }    return _appList;}- (void)viewDidLoad {    [super viewDidLoad];        [self appList];        for (int i = 0; i < self.appList.count; i++) {        App *app = self.appList[i];        NSLog(@"name = %@, icon = %@", app.name, app.icon);        UILabel *label = [[UILabel alloc] init];        label.frame = CGRectMake(10, 30 * (i+1), 150, 50);        label.text = app.name;        label.font = [UIFont systemFontOfSize:13];        [self.view addSubview:label];    }    }



 代码地址





0 0
原创粉丝点击