1. KVC Principle of implementation
- Iterate through all the keys in the dictionary, using name as an example
-
Go to the model to find there is no setname: method, there is a direct call assignment
-
If you do not find the SetName: method, you will continue to find there is no _name attribute, there is _name = value Assignment
-
If _name is not found, it will continue to look for the name attribute in the model
-
If you don't find it, you'll get a direct error.
Error message:
2. Use of the KVC principle
KVC is primarily used for model assignments, and model best defines the attributes we want, but sometimes it differs from the data we get, and here are a few common scenarios:
- When we fetch more data than the model, the above error occurs according to the KVC principle: override Setvalue:forundefinedkey in model's. m File: Method
"ZFFlag.h"@implementation zfflag-(void) SetValue: (ID) value Forundefinedkey: (NSString * ) key{}@end
- When we need the type of property and the type of data to be obtained, the workaround: Override the property's setter method (according to KVC's lookup order)
If you need a picture in the view, but the resulting data is usually the picture name, that is, a nsstring * type of data, the specific solution please see the code
#import <Foundation/Foundation.h>#import <UIKit/UIKit.h>@interface zfflag:nsobject@property (nonatomic, Strong) NSString *name; @property (nonatomic, Strong) UIImage *icon; rewrite the icon's setter method, noting that the type is the type of the property in the resulting data-(void) SetIcon: (NSString *) icon{_icon = [UIImage imagenamed: Icon];} // anti-collapse-(void) SetValue: (ID) value Forundefinedkey: (NSString *) key{}@end
#import"ZFFlagView.h"#import"ZFFlag.h"@interface Zfflagview () @property (weak, nonatomic) iboutlet UILabel *label; @property (Weak, nonatomic) Iboutlet Uiimageview *ImageView; @end@implementation zfflagview-(void) Setflag: (Zfflag *) flag{_flag = flag; // Assign a value to the child control _label.text = flag.name; _imageview.image = Flag.icon;} @end
Plane ticket:http://www.cnblogs.com/gfxxbk/p/5518270.html
KVC Principles of iOS Learning