-(void) Viewdidload {
[Super Viewdidload];
Additional setup after loading the view, typically from a nib.
Use of 1.KVC
The abbreviation for key value coding, which indicates the code
Function: Accesses the property and instance variables of an object in a string-coded manner
Used in projects: using KVC to implement dictionary-to-model conversions in JSON
Car *car = [[Car alloc] init];
Car.type = @ "BMW";
Car.speed = 250;
NSLog (@ "type =%@,speed=%f", car.type,car.speed);
KVC Access Property One way (special use string to do key)
Key: Attribute name can do key, instance variable name can also do key
[Car setvalue:@ "Mini" forkey:@ "type"];
NSLog (@ "type=%@", [Car valueforkey:@ "type"]);
Modify the base type and pass in the NSNumber
[Car setvalue:@ (+) forkey:@ "Speed"];
NSLog (@ "speed =%f", [[Car valueforkey:@ ' speed '] doublevalue]);
[Car setvalue:@ (3.14) forkey:@ "_num"];
[Car show];
Note: If key does not exist, an exception is thrown
Workaround: Re-implement Setvalue:forundefinedkey:
Use of 1.2 keypath
Engine *engine = [[Engine alloc] init];
Car.engine = engine;
Set the power properties of car engine
[Car setvalue:@ (+) forkeypath:@ "Engine.power"];
NSLog (@ "power =%f", [[Car valueforkeypath:@ "Engine.power"] doublevalue]);
1.3 Dictionary and Object conversions
Nsdictionary *cardict = @{@ "type": @ "QQ",
@ "Speed": @ "180"};
[Car setvaluesforkeyswithdictionary:cardict];
NSLog (@ "type =%@,speed=%f", car.type,car.speed);
Use of 2.KVO
Key value Observer key monitor
Function: Monitor the change of an attribute of an object
Example: Car object, its speed property has changed, the instrument panel shows the velocity
Newcar = [[Car alloc] init];
newcar.speed = 0;
Monitoring
Function: Once the speed has changed, execute the specified method in self
[Newcar addobserver:self forkeypath:@ "Speed" options:nskeyvalueobservingoptionold| Nskeyvalueobservingoptionnew Context:nil];
Analog start
Newcar.speed = 50;
Newcar.speed = 100;
Newcar.speed = 120;
Newcar.speed = 120;
Newcar.speed = 120;
}
-(void) Observevalueforkeypath: (NSString *) KeyPath Ofobject: (ID) object change: (nsdictionary *) Change context: (void *) Context
{
static double prespeed = 0;
Show the new speed
Double speed = [[Object Valueforkeypath:keypath] doublevalue];
if (prespeed! = speed)
{
NSLog (@ "speed =%f", speed);
}
Prespeed = speed;
}
-(void) dealloc
{
[Newcar removeobserver:self forkeypath:@ "Speed"];
}
Simple use of KVO and KVC modes for IOS development