Simple use of KVO and KVC modes for IOS development

Source: Internet
Author: User

-(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

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.