Objective-c (17, KVC key value code and case description)--ios Development Foundation

Source: Internet
Author: User

Combining the previous study notes and referring to the full solution of Objective-c Programming (third edition), this paper summarizes the objective-c knowledge points. Knowledge points have been changing, just as a reference to the official Apple document ~

17. Key Value Code KVC

The knowledge about KVC will be expanded by the following examples:

Person.h file, the person class has name and age two member variables

@interface Person : NSObject {     @private     NSString *_name;     NSInteger _age; } - (void)setAge:(NSInteger@end

PERSON.M implementation of this method

@implementation Person - (NSString *)description {     return [NSString stringWithFormat:@"name:%@,age=%li", _name, _age]; } - (void)setAge:(NSInteger)age {     @end

Main.m

//  p->_name = @"Zane";   不可访问//1.使用KVC给private的属性设置值 [p setValue:@"Zane" forKey:@"name"//2.通过KVC获取到属性值NSString *name = [p valueForKey:@"_name"NSLog(@"%@", name); [p setValue:@21 forKey:@"age"NSLog(@"%@", p);

Output

2015-07-09 17:29:43.477 exercise_KVC[579:24344] Zane2015-07-09 17:29:43.478 exercise_KVC[579:24344] name:Zane,age=21

1. Key-value encoding (key-value coding): Uses the string representing the information contained by the object as a key value to indirectly access the information.

Basically, whenever an accessor method, declaration attribute, or instance variable exists, its name can be accessed as a string . (even if the variable is @private private, it can be accessed)

2. Set the property value:

 - (void)setforsetValue:@"Zane"forKey:@"name"];

A, if the name is wrong here, written nama,nam1e and so on, the compiler will not error, but will cause the run crash
B,value is the ID type, so if the basic data type needs to be wrapped

Get property value

 - (id)valueForKey:(NSStringNSString *name = [p valueForKey:@"_name"];

If the person also has a dog, in a @class way to add in the Person.h, the dog has the Dogname attribute, You can specify the path settings

setValue:@"wangwang"forKeyPath:@"dog._dogname"];NSLog(@"%@",[p valueForKeyPath:@"dog._dogname"])
- (id)valueForKeyPath:(NSString *)keyPath;- (void)setValue:(id)value forKeyPath:(NSString *)keyPath;

3. Disadvantages
(1) The object-oriented encapsulation is destroyed ;
(2) The compiler does not keyvalue the set of errors;
(3) You need to parse the string first, the execution efficiency is lower than the set and get methods

4. Other instructions
(1) Use KVC to set properties, first go to the current class to find out if there is a corresponding set method , if there is a set method setting, if not found property settings
To call only write age cannot write _age
For example, in the example

setValue:@21forKey:@"age"setValue:@21forKey:@"_age"];

You can set breakpoints at the set method implementation of age, and you will find that if write _age does not call the Set method

(2) A one-to-many relationship
It is usually packaged into Nsarray, and then by the SetValue method, as value assignment.
Access to Valueforkeypath access via path access

(3) Numerical calculation
Increase the book member variable for person in the above example;
And Dog.h has member variables Dogage

@interface Dog : NSObject {     int _dogage; } - (instancetype)initWithAge:(int)age;   //并实现@end

Main.m

dog* dog1 = [[Dog alloc] Initwithage: -]; dog* dog2 = [[Dog alloc] Initwithage: -]; dog* dog3 = [[Dog alloc] Initwithage:Ten];Nsarray*array = [NsarrayArraywithobjects:dog1,dog2,dog3,Nil]; [P Setvalue:array forkey:@"Dog"];//Previous defined NSLog(@"%@", [P valueforkeypath:@"[email protected]"]);Number of//dog NSLog(@"%@", [P valueforkeypath:@"[Email protected]_dogage]]);//All dogage sum NSLog(@"%@", [P valueforkeypath:@"[Email protected]_dogage]]);//Average NSLog(@"%@", [P valueforkeypath:@"[Email protected]_dogage]]);//min value NSLog(@"%@", [P valueforkeypath:@"[Email protected]_dogage]]);//Maximum value

Output

2015-07-0917::. 479 EXERCISE_KVC[579:24344]32015-07-0917::. 479 EXERCISE_KVC[579:24344]602015-07-0917::. 480 EXERCISE_KVC[579:24344]202015-07-0917::. 480 EXERCISE_KVC[579:24344]102015-07-0917::. 480 EXERCISE_KVC[579:24344]30

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Objective-c (17, KVC key value code and case description)--ios Development Foundation

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.