The order in which KVC obtains the corresponding value through the key

Source: Internet
Author: User

KVC, that is, nskeyvaluecoding, uses a name as a key to access object attributes. In essence, KVC provides an alternative to the accesser to some extent, as long as it is possible that KVC tries its best to use the accesser method.

In the key example of the returned object property, KVC searches for the return value in the following order:

1.-(<type>) getkey accessors Method

2.-(<type>) Key

3. Call valueforundefinedkey: method. The default implementation of these methods throws an exception.

4. An nsundefinedkeyexception error is thrown.

 

KVC can simplify the code in some cases, as shown below:

// Implementation of data-source method without key-value coding- (id)tableView:(NSTableView *)tableview      objectValueForTableColumn:(id)column row:(NSInteger)row {     ChildObject *child = [childrenArray objectAtIndex:row];    if ([[column identifier] isEqualToString:@"name"]) {        return [child name];    }    if ([[column identifier] isEqualToString:@"age"]) {        return [child age];    }    if ([[column identifier] isEqualToString:@"favoriteColor"]) {        return [child favoriteColor];    }    // And so on.}//Implementation of data-source method with key-value coding- (id)tableView:(NSTableView *)tableview      objectValueForTableColumn:(id)column row:(NSInteger)row {     ChildObject *child = [childrenArray objectAtIndex:row];    return [child valueForKey:[column identifier]];}

 

 

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.