Value and assignment of KVC

Source: Internet
Author: User

. m file

1 #import<Foundation/Foundation.h>2 #import "Person.h"3 intMainintargcConst Char*argv[]) {4 @autoreleasepool {5         /*6 KVC: Full name Key value Coding is an indirect way to access an object instance variable to assign a value to the corresponding instance variable in the object7 with Setvalue:forkey:8 Assignment principle: Given a key, such as name9 1. First find the object has no corresponding SetName method, if there is a direct call to the method to assign value, if there is no go to step 2Ten 2. If there is no defined instance variable for _ then, if you have to assign a value directly to the instance variable, go to step 3 One 3. The Lookup object has no definition instance variable called name, if you have to assign a value directly to the instance variable, otherwise go to step 4 A 4. Method Setvalue:forundefinedkey in the auto-call object: - KVC Value procedure: Call Valueforkey: Given a key such as name - 1. The Lookup object has no corresponding getter method, if any, call the method to return the value of the corresponding instance variable, if there is no go to step 2 the 2. Then find out if you have defined the corresponding _name instance variable, if you have the value stored directly out of the instance variable, otherwise go to step 3 - 3. Then find out if the corresponding name instance variable is defined, and if there is a value stored directly out of the instance variable, go to step 4 - 4. Method Value:forundefindkey in the auto-call object: -           +          */ -         //KVC Assignment Process +Person *person =[[Person alloc] init]; A[Person SetValue:@"pilaf King"Forkey:@"name"]; at[Person SetValue:@"Monkey King"Forkey:@"name"]; -          -NSLog (@"%@", person.name); -         //KVC The process of taking value -NSLog (@"%@", [Person Valueforkey:@"name"]); -         //Setvalue:forkeypath: Assigning or taking values to instance variables based on path in[Person SetValue:@"Lele"Forkeypath:@"Person.name"]; -NSLog (@"%@", [Person Valueforkeypath:@"Person.name"]); to         //use KVC to encapsulate dictionaries into objects +Nsdictionary *dict = @{@"name":@"Xiao Ming",@"Sex":@"male",@"Address":@"you guessed"}; -         //person.name = [dict objectforkey:@ "name"]; the         //person.sex = [dict objectforkey:@ "Sex"]; *         //person.address = [dict objectforkey:@ "Address"]; $ Panax Notoginseng [Person setvaluesforkeyswithdictionary:dict]; -NSLog (@"%@", person); the          +     } A     return 0; the}

Use KVC to encapsulate dictionaries into objects that are used to assign values to objects directly.

Person.h file

1 #import <Foundation/Foundation.h>23@interface  person:nsobject 4 @property (nonatomic, retain) NSString *name; 5 @property (nonatomic, retain) nsstring *sex; 6 @property (nonatomic, retain) NSString *address; 7 @end

PERSON.M file

1 @implementation Person2-(ID) Valueforundefinedkey: (NSString *) Key3 {4NSLog (@"Lingling said she hit the code and found her wife.");5     returnNil;6 }7 //if there is no corresponding key, then the system automatically calls the following method, the method defaults to throw an exception, so that the system crashes.8 //If you don't want the method to throw an exception, then we need to rewrite this method ourselves.9-(void) SetValue: (ID) value Forundefinedkey: (nonnull NSString *) KeyTen { OneNSLog (@"not stupid, not yet."); A      - } - //override this method to output data from the original object (not Unicode character encoding) the-(NSString *) Description - { -     return[NSString stringWithFormat:@"name =%@,sex =%@, address =%@", self.name,self.sex,self.address]; - } + @end



Value and assignment of 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.