[Basic iOS control and basic ios Control
A. disadvantages of using dictionaries to load data 1. the User specifies the key by himself, which is prone to errors. 2. key is required for both storage and retrieval, which is easy to confuse B. model (model in MVC) 1. dictionary and model comparison: (1) dictionary: data storage, using string-type key values (easy to write wrong, wrong key compiler will not report an error) (2) model: data storage, custom Attributes store data. In fact, they are similar to JavaBean. The essence is Data encapsulation. implementation (1) define model classes
1 @ interface App: NSObject 2 3/** 4 copy: NSString 5 strong: general object 6 weak: UI control 7 assign: basic Data Type 8 */9 10/** 11 name 12 */13 @ property (nonatomic, copy) NSString * name; 14 15/** 16 icons 17 */18 @ property (nonatomic, copy) NSString * icon; 19 @ end
(2) read and store data using models:
1 # pragma mark get Application List 2-(NSArray *) apps {3 if (nil = _ apps) {4 // 1. obtain the full path of plist 5 NSString * path = [[NSBundle mainBundle] pathForResource: @ "app. plist "ofType: nil]; 6 7 // 2. load data 8 NSArray * dictArray = [NSArray arrayWithContentsOfFile: path]; 9 10 // 3. convert all the dictionaries in dictArray into a model and put them in the new array. 11 NSMutableArray * appArray = [NSMutableArray array]; 12 for (NSDictionary * dict in dictArray) {13 // 3.1 create model object 14 App * app = [[App alloc] init]; 15 16 // 3.2 assign all attributes of the dictionary to model 17 app. name = dict [NAME_KEY]; 18 app. icon = dict [ICON_KEY]; 19 20 // 3.3 Add to app array 21 [appArray addObject: app]; 22} 23 24 _ apps = appArray; 25} 26 27 return _ apps; 28}Read:
1 iconView. image = [UIImage imageNamed: appData. icon]; 2 [appView addSubview: iconView]; 3 4 // 2. set APP name 5 UILabel * nameLabel = [[UILabel alloc] init]; 6 nameLabel. text = appData. name;
C. the Model improves data parsing and storage in ViewControl, and adds coupling between data and classes. The controller cares too much and it is difficult to modify the Model after it is modified. solution: ==> so parse data in Model 1. implementation (1) custom construction method declaration with data parameters:
1/** 2 custom constructor 3 initialize the model object through dictionary 4 */5-(id) initWithDictionary :( NSDictionary *) dictionary;
Implementation:
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 }Usage:
1 // 3.1 create model object 2 App * app = [[App alloc] initWithDictionary: dict]; 3 4 // 3.2 add to app array 5 [appArray addObject: app];
(2) further provide class methods
1 + (id) appWithDictionary :( NSDictionary *) dictionary {2 // use self to represent the class name instead of the real class name to prevent errors in subclass calls 3 return [self alloc] initWithDictionary: dictionary]; 4}D. replace "id" with "instancetype": indicates all types. However, when an inappropriate type pointer is used to point to an unmatched object, the compiler will not report errors or warnings, however, it is very likely that an error will occur during running. instantcetype: 1. can point to any type 2. the compiler can automatically check whether the variable type is correct based on the object Currently pointed to, and then issue a warning 3. can only be used as the return value, not as a parameter