Ray's playground

 

Key-Value Coding, Key-Value observing(Chapter 7 of Cocoa Programming for Mac OS X)

 1 #import <Foundation/Foundation.h>
 2 
 3 
 4 @interface AppController : NSObject 
 5 {
 6     int fido;
 7 }
 8 
 9 - (int)fido;
10 - (void)setFido:(int)x;
11 - (IBAction)incrementFido:(id)sender;
12 
13 @end

 

AppController.m
 1 #import "AppController.h"
 2 
 3 @implementation AppController
 4 
 5 - (id)init
 6 {
 7     [super init];
 8     [self setValue:[NSNumber numberWithInt:5] forKey:@"fido"];
 9     NSNumber *= [self valueForKey:@"fido"];
10     NSLog(@"fido = %@", n);
11     return self;
12 }
13 
14 - (int)fido
15 {
16     NSLog(@"-fido is returning %d", fido);
17     return fido;
18 }
19 
20 - (void)setFido:(int)x
21 {
22     NSLog(@"-setFido: is called with %d", x);
23     fido = x;
24 }
25 
26 - (IBAction)incrementFido:(id)sender
27 {
28     [self willChangeValueForKey:@"fido"];
29     fido++;
30     NSLog(@"fido is now %d", fido);
31     [self didChangeValueForKey:@"fido"];
32 }
33 
34 @end

 

 

posted on 2011-02-10 10:33  Ray Z  阅读(181)  评论(0编辑  收藏  举报

导航