KVC Principles of iOS Learning

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.