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創(chuàng)建模型對象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創(chuàng)建模型對象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 }
新聞熱點
疑難解答