Detailed examples of KVC in IOS


Detailed examples of KVC in IOS

KVC is the abbreviation of key value coding, which means key value coding. In IOS, a method is provided to indirectly access the property of an object by using the name of the property (that is, the key). In fact, we can see various attributes of the class through the class definition. Then we can access the attribute value of the object instantiated by the class by using the attribute name.

This method can access the properties of the object without using getter / setter methods. Because if a member variable of a class does not provide a getter / setter, the outside world will lose access to this variable. KVC provides an access method, which can be very powerful in some occasions.  

No more, the code:

@interface myPerson : NSObject 
  NSString *_name; 
  int   _age; 
  int   _height; 
  int   _weight; 

@interface testViewController : UIViewController 
@property (nonatomic, retain) myPerson *testPerson; 

- (void)testKVC 
  testPerson = [[myPerson alloc] init]; 
  NSLog(@"testPerson's init height = %@", [testPerson valueForKey:@"height"]); 
  [testPerson setValue:[NSNumber numberWithInt:168] forKey:@"height"]; 
  NSLog(@"testPerson's height = %@", [testPerson valueForKey:@"height"]); 

The first section of code defines a class of myperson, which has a_ The property of height, but it does not provide any access method of getter / setter. At the same time, there is a myperson object pointer in the testviewcontroller class.

When myperson is instantiated, it is generally impossible to access this object_ Height attribute, but we did it through KVC. The code is testkvc.

After running, the printed value is:

2013-11-02 11:16:21.970 test[408:c07] testPerson's init height = 0
2013-11-02 11:16:21.971 test[408:c07] testPerson's height = 168

This means that you do read and write_ The height property.  

Common methods of KVC:

- (id)valueForKey:(NSString *)key; 
- (void)setValue:(id)value forKey:(NSString *)key; 

The valueforkey method reads the properties of the object according to the value of the key. SetValue: forkey: writes the properties of the object according to the value of the key.  

Here are a few to emphasize

1. The value of key must be correct. If it is misspelled, an exception will appear
2. When the key value is undefined, valueforundefinedkey: this method will be called. If you write this method yourself, the key value will be called here if there is an error
3. Because the class key is nested repeatedly, there is a concept of keypath. Keypath is used To link keys one by one, so that you can access them according to this path
4. Both nsarray and nsset support KVC

The above is how to use IOS KVC. If you have any questions, please leave a message or go to the community of this site for communication and discussion. Thank you for reading. I hope it can help you. Thank you for your support to this site!

Recommended Today

Explanation of websocket heartbeat reconnection

Recently, websocket has been used in the development of applet. The applet provides corresponding native API, which is different from H5 API. Therefore, some mature class libraries of popular H5 are difficult to use, and the native API has some defects, so it implements a set of heartbeat reconnection mechanism by itself. First, let’s briefly […]