Objective-c Diary-The KVC

Source: Internet
Author: User

KVC (key-value-coding?) 1, overview

A method that sends or obtains a value as a string to an instance variable or property of an object.

2, usage

A, take the value

@property (readwrite,copy) nsstring * name; Car.h

@synthesize NAME;//CAR.M

Now, I want to get the value of name, you can use the KVC method

NSString *name=[car valueforkey:@ "name"];

b, set the value

[Car setvalue:@ "Harlod" forkey:@ "name"];

3, extension usage

A, use a path (string expression form dot expression)-(void) Setvalue:forkeypath

@interface Engine:nsobject <nscopying>{

int horsepower;

}

@end//engine

[Car Setvalue:[nsnumber numberwithint:155]

forkeypath:@ "Engine.horsepower"];

b, using a key value for Nsarray, the key value is found for each object in the array, and the query results are packaged back.

As follows:

Nsarray *pressures=[car valueforkeypath:@ "Tires.pressure"];

NSLog (@ "pressures%@", pressures);

Will output the following results:

Pressures (

34,

34,

34,

34

)

4, progressive usage

A, fast arithmetic, that is, using the escape operator, such as @count, @max, @min, @avg calculate the result on the left side of the key path.

As follows:

@interface garge:nsobject{

NSString *name;

Nsmutablearray *cars;

}

@property (readwrite,copy) NSString *name;

-(void) Addcar: (car *) car;

-(void) print;

@end//garage

NSNumber *count;

Count=[garage valueforkeypath:@ "[email protected]"];

NSLog (@ "We have%@ cars", count);

NSNumber *sum;

Sum=[garage valueforkeypath:@ "[email protected]"];

NSLog (@ "We have a grand total of%@ miles", sum);

b, Quick get or set (using dictionary)

-dictionarywithvaluesforkeys and-setvalueforkeyswithdictionary

The following (Get values):

Car=[[garage valueforkeypath:@ "Cars"] lastobject];//in the path of the key value is not a property in garage, but can also be used as a property path

First, pick a car from the garage, as above

Nsarray *keys=[nsarray arraywithobjects:@ "make", @ "model", @ "Modelyear", nil];

Then, a single list of properties that need to be obtained is placed in the keys array, as above

Nsdictionary *carvalues=[car Dictionarywithvaluesforkeys:keys];

Finally, use the word typical pair like store to get the dictionary value using-dictionarywithvaluesforkeys.

The following are (set values):

Nsdictionary *newvalues=[nsdictionary dictionarywithobjectsandkeys:@ "Chevy", @ "make" @ "Nova" @ "model", [NSNumber numberwithint:1964],@ "Modelyear", nil];

First, define a dictionary

[Car setvaluesforkeyswithdictionary:newvalues];

Again, set the value of the car.

c, handling undefined keys

When we use Valueforkey, we use an undefined keyword, and then we get an error at run time:

This class is not key value coding-compliant for the key xxxxxx.

When we deal with these kinds of problems, we generally use the rewriting-valueforundefinedkey and-setvalue:forundefinedkey methods

The following methods are used in the OBJECTIVE-C basic tutorial

C1, defines an empty Dictionary object stuff.

@interface garage:nsobject{

NSString *name;

Nsmutablearray *cars;

Nsmutabledictionary *stuff;

}

C2, handling, setting an assignment problem for undefined keys, placing key-value pairs in a Dictionary object

-(void) SetValue: (ID) value Forunderfinedkey: (NSString *) key{

if (Stuff=nill) {

Stuff==[[nsmutabledictionary Alloc]init];

}

[Stuff Setvaleu:value Forkey:key];

}//setvalueforundefinedkey

C3, processing, getting values for undefined keys, redirecting lookups to Dictionary objects

-(ID) Valueforundefinedkey: (NSString *) key{

ID Value=[stuff Valueforkey:key];

return (value);

}//valueforundefinedkey

Objective-c Diary-The KVC

Related Article

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.