对plist文件进行简单的封装

对plist文件进行简单的封装_第1张图片
+ (NSArray *)loadDataFromPlistWithName:(NSString *)plistName{
    NSString *path = [[NSBundle mainBundle] pathForResource:plistName ofType:@"plist"];
    NSArray *array = [NSArray arrayWithContentsOfFile:path];
    
    NSMutableArray *dataArray = [NSMutableArray array];
    
    for (int i = 0; i < array.count; i++) {
        NSDictionary *dic = [array objectAtIndex:i];
        //读取分组名
        NSString *groupName = [dic objectForKey:@"groupName"];
        //读取这一组的好友
        NSArray *friendsArray = [dic objectForKey:@"friends"];
        //封装每一个好友为PersonModel类型
        NSMutableArray *personArray = [NSMutableArray array];
        
        for (NSDictionary *personDic in friendsArray) {
            PersonModel *person = [[PersonModel alloc] initWithName:[personDic objectForKey:@"name"] iconName:[personDic objectForKey:@"icon"] introduction:[personDic objectForKey:@"intro"]];
            [personArray addObject:person];
        }
        NSDictionary *friendDic = @{@"friends":personArray, @"groupName":groupName};
        [dataArray addObject:friendDic];
    }
    
    return dataArray;
    
}

对plist文件进行简单的封装_第2张图片
+ (NSArray *)loadEmotionDataFromPlistWithName:(NSString *)plistName{
    NSString *path = [[NSBundle mainBundle] pathForResource:plistName ofType:@"plist"];
    NSArray *rootArray = [NSArray arrayWithContentsOfFile:path];
    
    NSMutableArray *dataArray = [NSMutableArray array];
    
    for (int i = 0; i < rootArray.count; i++) {
        NSDictionary *dic = [rootArray objectAtIndex:i];
        
        NSString *chs = [dic objectForKey:@"chs"];
        NSString *png = [dic objectForKey:@"png"];
        
        EmotionModel *emotion = [[EmotionModel alloc] initWithName:chs emotionImage:png];
        [dataArray addObject:emotion];
    }
    return dataArray;
}

你可能感兴趣的:(对plist文件进行简单的封装)