iOS Development-KVC

Source: Internet
Author: User

1. What is KVC

Kvc--key-value coding, Health value code

Method that can directly access the value of an object property via key

2. Usage Scenarios

KVC is primarily intended to make the code more concise.

More is used in the background data parsing, and access to some of the properties of the Setter,getter method

3. Code parsing:

Definitions are in NSKeyValueCoding.h.

The main two methods:

Key is the property name of the current class-(ID) Valueforkey: (NSString *) key;  When a class does not have a property named key, it calls the throw exception, which can be replicated Valueforundefinedkey: method, Handle-(void) SetValue: (ID) value Forkey: (NSString *) key;// When a class does not have a property named key, it is called to throw an exception, which can be replicated-(void) SetValue: (ID) value Forundefinedkey: (NSString *) Key: Method, which handles//keypath as the path to access the property, such as: @ " Person.name ", person is the property of the current class, name is a property name of the People class-(ID) Valueforkeypath: (NSString *) keypath;-(void) SetValue: (Nsvalue * ) value Forkeypath: (NSString *) KeyPath;

Exception handling

-(ID) Valueforundefinedkey: (NSString *) key;-(void) SetValue: (ID) value Forundefinedkey: (NSString *) key

code example:

@interface Viewcontroller () @property (nonatomic, weak) nsstring *viewstring; @end @implementation viewcontroller-(void ) Viewdidload {    [super viewdidload];    NSLog (@ "----[%@]", [Self valueforkey:@ "viewstring"]);    NSLog (@ "----[%@]", [Self valueforkey:@ "Viewarray"]);     [Self setvalue:@ "testforviewstring" forkey:@ "viewstring"];    [Self setvalue:@ "1" forkey:@ "Viewarray"]; }-(void) SetValue: (ID) value Forundefinedkey: (NSString *) key {    NSLog (@ "WARNING"!!!!!! ---undefined--key[%@]--!!!!!! ", key);} -(ID) Valueforundefinedkey: (NSString *) key {    if ([Key isequaltostring:@ "Viewarray"]) {        return @ "AAAA";    }    else {        return [super Valueforundefinedkey:key];    }}

iOS Development-KVC

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.