oc UITableView 2 解析数组(还没消化)

plist 解析

oc UITableView 2 解析数组(还没消化)_第1张图片

 oc UITableView 2 解析数组(还没消化)_第2张图片

 // 加载字典数组
        NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"cars" ofType:@"plist"]];
        NSLog(@"%@",dictArray);
        // 遍历
        NSMutableArray *temp = [NSMutableArray array];
        for(NSDictionary *carGroupDict in dictArray){
            XMGCarGroup *group = [XMGCarGroup carGroupWithDict:carGroupDict];
            [temp addObject:group];
        }
        _carGroups = temp;
//
//  XMGCarGroup.h
//  OcDemoTest
//
//  Created by Mac on 2023/7/16.
//

#import 



@interface XMGCarGroup : NSObject
@property (nonatomic,copy) NSString *header;
@property (nonatomic,copy)NSString *footer;

@property (nonatomic,copy) NSArray *cart;

+(instancetype)carGroupWithDict:(NSDictionary *)dict;



@end


//
//  XMGCarGroup.m
//  OcDemoTest
//
//  Created by Mac on 2023/7/16.
//

#import "XMGCarGroup.h"
#import "TableCar.h"

@implementation XMGCarGroup
+(instancetype)carGroupWithDict:(NSDictionary *)dict{
    XMGCarGroup *group = [[self alloc]init];
    //[group setValuesForKeysWithDictionary:dict];
    group.header = dict[@"header"];
    group.footer = dict[@"footer"];
    // 遍历数据
    // 字典数组 -> 模型数组
    NSMutableArray *temp = [NSMutableArray array];
    for(NSDictionary *carDict in dict[@"cars"]){
        TableCar *car = [TableCar carWithDict:carDict];
        [temp addObject:car];
        
        
    }
    group.cart = temp;
    
    return group;
}
@end
//
//  TableCar.h
//  OcDemoTest
//
//  Created by Mac on 2023/7/16.
//

#import 



@interface TableCar : NSObject
@property(nonatomic,copy) NSString *name;

@property(nonatomic,copy) NSString *icon;

//+(instancetype)carWithName:(NSString *)name icon:(NSString *)icon;

+(instancetype)carWithDict:(NSDictionary *)dict;

@end


//
//  TableCar.m
//  OcDemoTest
//
//  Created by Mac on 2023/7/16.
//

#import "TableCar.h"

@implementation TableCar
//+(instancetype)carWithName:(NSString *)name icon:(NSString *)icon{
//    TableCar *car = [[self alloc]init];
//    car.name = name;
//    car.icon = icon;
//    return car;
//}

+(instancetype)carWithDict:(NSDictionary *)dict{
    TableCar *car = [[self alloc]init];
    car.name = dict[@"name"];
    car.icon = dict[@"icon"];
    return car;
}
@end

你可能感兴趣的:(oc,ios)