Oc syntax kvo, ockvo

Source: Internet
Author: User

Oc syntax kvo, ockvo

Naming subject:

Kvo is a class that monitors the variables of another class and notifies this class when the Variables change.

For example:

Create a class for a person. The class for a person has a bank card. The class for creating a bank card has a variable for money;

Let's use people's classes to monitor the amount of money in the bank card, and notify people when the amount of money in the bank card changes;

The following is an example;

Create


Create a person class

Person. h

# Import <Foundation/Foundation. h> @ class BankCard; @ interface Person: NSObject {BankCard * bancard; // bank card} @ end

Person. m

# Import "Person. h "# import" BankCard. h "@ implementation Person // rewrite the initialization function-(id) init {self = [super init]; if (self) {bancard = [[BankCard alloc] init]; // Add a listener with the old value and new value of the money variable in bancard [bancard addObserver: self forKeyPath: @ "money" options: NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context: NULL];} return self;} // This method is automatically called when the value of the listener changes-(void) observeValueForKeyPath :( NSString *) keyPath ofObject :( id) object change :( NSDictionary *) change context :( void *) context {if ([keyPath isEqual: @ "money"]) {NSString * new = [change objectForKey: @ "old"]; NSString * old = [change objectForKey: @ "new"]; NSLog (@ "% @", new, old);} @ end

Bank Card Type:

BankCard. h

#import <Foundation/Foundation.h>@interface BankCard : NSObject@property (nonatomic,assign) float money;@end
BankCard. m

# Import "BankCard. h "@ implementation BankCard-(id) init {self = [super init]; if (self) {_ money = 10.0f; // The timer is used here. It doesn't matter if you don't understand it, you can call the balanceUpdate function every second to change the value of money so that we can check whether the listening is successful. [nst1_scheduledtimerwithtimeinterval: 1.0f target: self selector: @ selector (balanceUpdate) userInfo: nil repeats: YES];} return self;}-(void) balanceUpdate {float f = self. money; f + = arc4random () % 100; // generates a random number self from 0 to 99. money = f ;}@ end

Main. m

#import <Foundation/Foundation.h>#import "Person.h"int main(int argc, const char * argv[]){    @autoreleasepool {                // insert code here...        NSLog(@"Hello, World!");        Person *p = [[Person alloc]init];        [[NSRunLoop currentRunLoop]run];    }    return 0;}


Running result:

2014-07-25 21:51:32.210 IOS KVO[3388:303] Hello, World!2014-07-25 21:51:33.214 IOS KVO[3388:303] 10   932014-07-25 21:51:34.213 IOS KVO[3388:303] 93   1542014-07-25 21:51:35.214 IOS KVO[3388:303] 154   1822014-07-25 21:51:36.213 IOS KVO[3388:303] 182   2142014-07-25 21:51:37.213 IOS KVO[3388:303] 214   3072014-07-25 21:51:38.213 IOS KVO[3388:303] 307   3582014-07-25 21:51:39.213 IOS KVO[3388:303] 358   4042014-07-25 21:51:40.213 IOS KVO[3388:303] 404   4932014-07-25 21:51:41.213 IOS KVO[3388:303] 493   542Program ended with exit code: -1

Refer to the video from qianfeng.


Analysis of sat oc answers !!! (Grammar, reading, and mathematics are required)

I am too lazy to decompress the files and upload them one by one. There are OG and OC in the files. After decompression, the OC folder contains the oc questions and answers.


In iOS development, what is the sequence of OC syntax specifications?

Self. label. text = indicates the value of the text field of a label control object in this class,

Self this class definition contains UIlabel instance object, UIlabel class has the text attribute.
Text. self. labe = @ "hello world "? Text is the most basic attribute.

Related Article

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.