1 @interface App : NSObject 2 3 /** 4 copy : NSString 5 strong: 一般对象 6 weak: UI控件 7 assign: 基本数据类型 8 */ 9 10 /**11 名称12 */13 @PRoperty(nonatomic, copy) NSString *name;14 15 /**16 图标17 */18 @property(nonatomic, copy) NSString *icon;19 @end
1 #pragma mark 取得应用列表 2 - (NSArray *) apps { 3 if (nil == _apps) { 4 // 1.获得plist的全路径 5 NSString *path = [[NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil]; 6 7 // 2.加载数据 8 NSArray *dictArray = [NSArray arrayWithContentsOfFile:path]; 9 10 // 3.将dictArray里面的所有字典转成模型,放到新数组中11 NSMutableArray *appArray = [NSMutableArray array];12 for (NSDictionary *dict in dictArray) {13 // 3.1创建模型对象14 App *app = [[App alloc] init];15 16 // 3.2 将字典的所有属性赋值给模型17 app.name = dict[NAME_KEY];18 app.icon = dict[ICON_KEY];19 20 // 3.3 添加到app数组中21 [appArray addObject:app];22 }23 24 _apps = appArray;25 }26 27 return _apps;28 }
1 iconView.image = [UIImage imageNamed:appData.icon];2 [appView addSubview:iconView];3 4 // 2.设置APP名字5 UILabel *nameLabel = [[UILabel alloc] init];6 nameLabel.text = appData.name;
1 /**2 自定义构造方法3 通过字典来初始化模型对象4 */5 - (id) initWithDictionary:(NSDictionary *) dictionary;
1 - (id) initWithDictionary:(NSDictionary *) dictionary {2 if (self = [super init]) {3 self.name = dictionary[NAME_KEY];4 self.icon = dictionary[ICON_KEY];5 }6 7 return self;8 }
1 // 3.1创建模型对象2 App *app = [[App alloc] initWithDictionary:dict];3 4 // 3.2 添加到app数组中5 [appArray addObject:app];
1 + (id) appWithDictionary:(NSDictionary *) dictionary {2 // 使用self代表类名代替真实类名,防止子类调用出错3 return [[self alloc] initWithDictionary:dictionary];4 }
新闻热点
疑难解答