Transferred from: http://my.oschina.net/caijunrong/blog/510701
First, for KVC mode (Key Value Coding):
1, in fact, in the actual development of the use of more than is: After receiving the JSON data, through parsing, parsing into nsdictionary, and then the dictionary corresponding to the field to create a model, in the model inside a custom class method + (instancetype) Modelwithdictionary: (Nsdictionary *) called in Keydictionary method
[self setValuesForKeysWithDictionary:jsonObject];
So as to achieve the effect we want, the dictionary is loaded into model.
2, and then for some of the fields that you want to special processing can call the following methods for special processing, such as some of the key is a dictionary type, you can be done in the following ways:
-(setvalue: (ID) value forkey: (NSString * ) Key { if ([Key isEqualToString : @ "products"]) { for (Nsmutabledictionary *productdict in Value) { prodcut *product = [[product alloc] initwithdictionary:prodcutdict]; [self.products addobject:product]; } } }
3, there is another situation, is that the key is not defined at all, you can rewrite the following method to re-key and value corresponding to the number:
- (void) setvalue: (ID) Value forundefinedkey: (nsstring *) key { if ([Key isequaltostring:@ " Namexxx "]) self.name = value; if ([Key isequaltostring:@ "agexxx"]) self.age = value; else [ super setvalue:value forkey:key]; }
Second, for the KVO design mode (KVO simple use):
Key Value observing, as the name implies is a observer mode for monitoring property changes, KVO and nsnotification have a lot of similar places, with AddObserver:forKeyPath:options : Context: Go to start observer, with REMOVEOBSERVER:FORKEYPATH:CONTEXT to stop Observer, callback is Observevalueforkeypath:ofobject: Change:context:.
KVO's implementation also relies on OBJECTIVE-C's powerful Runtime.
A simple overview of the implementation of KVO:
When you look at an object, a new class is created dynamically. This class inherits from the original class of the object and overrides the setter method of the observed property. Naturally, the overridden setter method is responsible for notifying the changes to all observed object values before and after invoking the original setter method. Finally, the object's ISA pointer (the ISA pointer tells the Runtime system what the object's class is) points to the newly created subclass, and the object magically becomes an instance of the newly created subclass.
Originally, this intermediate class, inherited from the original class. Not only that, Apple has rewritten the-class method, trying to deceive us that this class has not changed, that is the original class. More specific information, go run the code in the Mike Ash article to understand that there is no repetition here.
Here's a sample code that comes from another blog:
@interface StockData : NSObject { NSString * stockName; float price; } @end @implementation StockData @end
- (void) Viewdidload {[Super Viewdidload]; STOCKFORKVO = [[StockData alloc] init]; [Stockforkvosetvalue:@"Searph"forkey:@"StockName"]; [Stockforkvosetvalue:@"10.0"forkey:@"Price"]; [StockforkvoAddobserver:selfforkeypath:@"Price"options:nskeyvalueobservingoptionnew| NskeyvalueobservingoptionoldContext:null]; MyLabel = [[UILabel alloc]Initwithframe:cgrectmake (100,100,100,30)]; Mylabel.textcolor = [Uicolor Redcolor]; MyLabel.Text = [Stockforkvovalueforkey:@ "price"]; [self.view addsubview:mylabel]; uibutton * b = [uibutton buttonWithType: Uibuttontyperoundedrect]; b.frame = cgrectmake ( 0, 0, 100, 30) [b addTarget:self action: @selector (buttonaction) forcontrolevents:uicontroleventtouchupinside]; [self.view ADDSUBVIEW:B];    }
-(setvalue:@ "20.0" forKey:@< Span class= "hljs-string" > "price"];&NBSP;&NBSP;}
-(observevalueforkeypath: (nsstring *) Keypath Ofobject: (ID) object change: (nsdictionary *) change context: (void *) context { if ([Keypath isEqualToString: @ "price"]) { mylabel.text = [stockforkvo valueForKey:@< Span class= "hljs-string" > "price"];&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;}&NBSP;&NBSP;}
- (void)dealloc { [stockForKVO removeObserver:self forKeyPath:@"price"]; }
iOS Development written interview-Kvc/kvo Simple to use