2. dictionary-to-model and xib, iosxib for iOS development
I. dictionary-to-model (plist file)
Model encapsulation method: (put in the code repository)
1. Add the property name corresponding to the key value of the plist file to the. h file.
Array --- > NSArrayDictionary --- > NSDictionarystring ---> NSStringnumber ---> NSNumber
Note: The property name and the key value in plist must be consistent.
2. Use of strong weak copy assign in the H file
1> strong: used for general objects or custom object types 2> weak: used for UI controls and delegate3> copy: used for NSString block objects 4> assign: used for basic data types
3. Add a constructor in. h that can pass in dictionary parameters.
- (instancetype)initWithDict:(NSDictionary*)dict;+ (instancetype)xxxWithDict:(NSDictionary*)dict;
4. Implement the corresponding constructor in the. m file
// In initWithDict: Use KVC to get all attributes
5. Differences between instancetype and id
1> similarities: both can be used as the return value type of the method.
2> difference: the instancetype compiler detects the true type, and the id can be used on the parameter type.
Ii. KVC
KVC-key-value encoding is a method for indirectly modifying/reading object attributes.
1. Usage considerations:
1> the key value name in plist must be consistent with the attribute in the model. 2> the attribute in the model may not appear completely in plist, however, if the properties in the plist file do not appear completely in the properties, an error occurs.
Iii. Use of xib
The xib file is used to describe a partial UI.
In the development phase, xib files are oriented to developers. When xib files are installed on mobile phones, they are converted to nib files.
1. How to load xib files
1> Method 1: create all the xib objects and put them in the objs array in order.
NSArray * objs = [[NSBundle mainBundle] loadNibNamed:@"appView" owner:nil options:nil];
2> method 2
UINib *nib = [UINib nibWithNibName:@"appView" bundle:[NSBundle mainBundle]];NSArray* objs = [nib instantiateWithOwner:nil options:nil];
2. Process of encapsulating xib into view
1> create a custom view with the integrated UIView. Suppose the class name is WBAppView. 2> Create a WBAppView. xib file to describe the internal structure of WBAppView 3> change the xib type to the actual type of WBAppView 4> link the internal child control with WBAppView 5> WBAppView provides a model attribute 6> rewrite Model set Method of attribute, because the set method can obtain externally transmitted model data 7> split the model data and set the data to the corresponding child control 8> supplement: provides a class method for creating WBAppView, block the read xib File Code
4. Lazy Loading
- Lazy loading: It is loaded only when it is used. It is implemented in the get method. The get method in OC is to remove the first letter of the underline attribute and make it lowercase.
When used: real data or get method (document)
-(NSArray *) apps {if (_ apps = nil) {// 1. obtain the full path NSString * path = [[NSBundle mainBundle] pathForResource: @ "app. plist "ofType: nil]; // 2. load the array NSArray * dictArray = [NSArray arrayWithContentsOfFile: path]; // 3. convert all the dictionaries in dictArray into model objects and put them in the new array. NSMutableArray * appArray = [NSMutableArray array]; for (NSDictionary * dict in dictArray) {// 3. 1. create model object MJApp * app = [MJApp appWithDict: dict]; // 3. 2. add the model object to the array [appArray addObject: app];} // 4. value assignment _ apps = appArray;} return _ apps ;}
Supplement: do not interact with controls
1> alpha <= 0.012> hidden = YES3> userInteraction = NO (userInteraction = NO of the parent Control) 4> the location exceeds the size of the parent control. // The parent view cannot interact, sub-views cannot use naming rules for interaction methods either:
Naming rules:
1> lowercase letters, followed by uppercase letters 2> initWithxxx: init initialization function With words must be capitalized, because OC defaults to "camper ", the default initialization function starts with the init word. If initwithxxx is considered to start with the initwith word, the initialization method cannot be found. 3> method, and the attribute cannot start with the new name.
''
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.