Overview: In all [KVC] key-Value Coding based
Key-Value Coding (KVC) to achieve analysis
KVC use of a isa-swizzling technology. isa-swizzling is the type of mixing pointer mechanism. KVC mainly through isa- swizzling, to achieve its internal lookup location. isa pointer, as its name implies, (that is a kind of mean), the class of object points distribution table maintenance. The distribution table is actually directed to a method comprising the implementation class pointers, and other data.
For example, the following line of code KVC:
[site setValue:@"sitename" forKey:@"name"];
The compiler will be treated to:
SEL sel = sel_get_uid ("setValue:forKey:");
IMP method = objc_msg_lookup (site->isa,sel);
method(site, sel, @"sitename", @"name");
Introduces two basic concepts:
(1) SEL Data type: it is a compiler running environment parameter in the Objective-C method.
(2) IMP data type: he is actually inside a compiler implements function pointer time. When the Objective-C compiler to implement a processing method when an IMP will point object that is expressed in the C language type (in fact, when the Objective-C compiler process, basically C language of).
This time it was clear KVC internal implementation of clear: an object when calling the setValue, (1) First, according to the method name to find the method of operation when the environmental parameters required. (2) he will be combined with environmental parameters from their own isa pointer, the interface to find specific ways to achieve. (3) re-direct to find specific ways come to realize.Overview Key-Value Observing mechanism
Key-Value Observing (abbreviated as KVO): When the attributes of the specified object is modified, allowing the object upon receiving notification mechanism. Each time the specified object's properties are observed when the modified, KVO are automatically notified to the respective observer.
KVO advantage
When the property has changed, KVO will provide automatic message notification. This architecture has many benefits. First, developers do not need to go to achieve this program: Each attribute has changed to send a message notification. This is the biggest advantage KVO mechanisms. Because this program has been clearly defined, to obtain framework-level support, it can easily adopt. Developers do not need to add any code, do not need to design their own observer model, it can be used directly in the project. Secondly, KVO architecture is very powerful, can easily support multiple observers observe the same property, and their associated values.
KVB
two basic approaches
1: Add the observer OBserver objectaddObserver:forKeyPath:options:context:
2: handler receives viewer information OBserverobserveValueForKeyPath:ofObject:change:context:
Use steps as follows:
1. Registration, specify the attribute of the viewer,
2. implement callbacks Method
3. Trigger callback method
4. Remove observed
KVO example using the following code:
############## # the model (model) ###############
#import <Foundation / Foundation.h>
@interface Music: the NSObject { // attributes listening NSString * musicName; } @end #import " Music.h " @implementation Music @end ############### the ViewController (view controller) ############### #import <the UIKit / UIKit.h> @class Music; @interface the ViewController: the UIViewController { Music Music *; } @Property (nonatomic, The retain) Music * Music; @end
the ViewController @implementation
@synthesize Music;
#pragma Mark - View Lifecycle
- (void) the viewDidLoad
{ [Super the viewDidLoad]; Music = [[Music the alloc] the init]; // add registered observers when properties change are called [ addObserver Music: Self forKeyPath: @ "musicName" Options: NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context: nil]; // UILabel controls UILabel * musicLabel = [[UILabel alloc] initWithFrame: CGRectMake (20, 150, 280, 21)]; musicLabel.font = [UIFont fontWithName: @ "ArialMT" size: 18 is]; musicLabel.textColor = [UIColor redColor]; musicLabel.tag = 100; [self.view addSubview: musicLabel]; [musicLabel Release]; // controls the UITextField
UITextField *musicTextField = [[UITextField alloc] initWithFrame:CGRectMake(20, 200, 280, 21)];
musicTextField.font = [UIFont fontWithName:@"ArialMT" size:18];
musicTextField.placeholder = @"Please enter some words.";
musicTextField.backgroundColor = [UIColor whiteColor];
// UITextField输入内容时候调用
[musicTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
[self.view addSubview:musicTextField];
[musicTextField release];
self.view.backgroundColor = [UIColor grayColor];
}
- (void)textFieldDidChange:(id)sender {
UITextField *textField = (UITextField *)sender;
NSLog(@">>>>>>>>>>>>>>>%@",textField.text);
// modify the property is listening, the callback methods are invoked under [Music setValue: TextField.text forKey: @ "musicName"]; } // as long as the "musicName" Music class of property changes that occur will trigger the following methods - ( void) the observeValueForKeyPath: (NSString *) keyPath ofObject: (ID) Object Change: (NSDictionary *) Change context: (void *) context { UILabel * label = (UILabel *) [self.view viewWithTag: 100]; // if attribute change is "musicName" IF ( [keyPath as isEqualToString: @ "musicName"] ) { // the value of the current musicName attributes assigned to UILabel Label.text = [Music valueForKey: @ "musicName"] ; // output change value before NSLog (@ "% @ Old musicName iS", [Change objectForKey: @ "Old"] ); // output value changed
NSLog(@"new musicName is %@",[change objectForKey:@"new"]);
}
}
#pragma mark - Memory Management
- (void)dealloc {
// 移除观察者
[music removeObserver:self forKeyPath:@"musicName"];
[music release];
[super dealloc];
}
MVC architecture is an acronym for "model-view-controller", the Chinese translation is "model - view - controller." MVC application always consists of three parts. Event (events) lead to changes in Model or Controller View, change, or both at the same time. Controller changed as long as the data or attributes Models, View all dependent updated automatically. Similarly, as long as Controller changed View, View gets the data from the underlying Model to refresh themselves.
Smalltalk MVC architecture was first proposed language study group, applied to the user interactive applications.
MVC pattern is a complex architecture model, its implementation is very complicated. However, we have summarized a lot of reliable design patterns, a variety of design patterns together, the realization of the MVC pattern becomes relatively easy. Views can be seen as a tree, apparently with the Composite Pattern can be implemented. The relationship between Views and Models may reflect the use Observer Pattern. Controller Control Display Views, you can use Strategy Pattern achieve. Model usually a mediator, may be employed to achieve Mediator Pattern.
KVC - KVO - KVB advantage
These mechanisms by specifying a common set of Cocoa nomenclature, calling conventions, etc., to achieve the following functions:
Reproduced in: https: //my.oschina.net/dake/blog/196740