标签:
-----------------------main-------------------------------
1 <font size="3"><font size="3">#import <Foundation/Foundation.h> 2 #import "Student.h" 3 4 int main(int argc, const char * argv[]) { 5 @autoreleasepool { 6 7 Student* s = [[Student alloc] init]; 8 [s setValue:@"语文" forKeyPath:@"course.courseName"]; 9 10 [s print]; 11 12 //键:必须与实例变量同名 13 [s setValue:@"lily" forKey:@"name"]; 14 [s print]; 15 [s setValue:[NSNumber numberWithInt:10] forKey:@"age"]; 16 [s print]; 17 18 //对课程设置 19 Course* c = [[Course alloc] init]; 20 [c setValue:@"math" forKey:@"courseName"]; 21 22 [s setValue:c forKey:@"course"]; 23 NSLog(@"%lu",[c retainCount]); 24 25 //对s的课程名称进行修改 26 [s setValue:@"English" forKeyPath:@"course.courseName"]; 27 [s print]; 28 [s valueForKeyPath:@"course.courseName"]; 29 30 31 NSLog(@"%@",[s valueForKey:@"name"]); 32 NSLog(@"%@",[s valueForKey:@"age"]); 33 34 [c release]; 35 [s release]; 36 37 //脱字符:^ 38 //块的返回值类型(^块的名字)(形参列表); 39 //块的实现基本格式。 40 //^(形参列表){//实现语句} 41 __block int a = 20;//被__block修饰的变量可以在块中被修改,否则只能读取值 42 a++; 43 44 int(^myblock)(int); 45 myblock = ^(int n){ 46 NSLog(@"块"); 47 return n+1; 48 }; 49 NSLog(@"%d",myblock(10));//使用块。 50 51 } 52 return 0; 53 } 54 </font></font>
-----------------------Student.h-------------------------------
1 <font size="3">#import <Foundation/Foundation.h> 2 #import "Course.h" 3 4 //kvc kvo:course.courseName 学生:观察者 5 6 @interface Student : NSObject 7 { 8 NSString* name; 9 int age; 10 Course* course; 11 } 12 13 -(void)print; 14 15 @end</font>
-----------------------Student.m-------------------------------
1 <font size="3">#import "Student.h" 2 3 @implementation Student 4 5 -(id)init 6 { 7 if (self = [super init]) { 8 name = [[NSString alloc] initWithFormat:@"lily"]; 9 course = [[Course alloc] init]; 10 age = 0; 11 } 12 //添加观察者, 观察者:学生 观察的实例变量:course.courseName 13 [course addObserver:self forKeyPath:@"courseName" options:NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew context:NULL]; 14 return self; 15 } 16 17 18 //一旦观察者观察的实例变量变化,此方法会被回调 19 20 -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context 21 { 22 // NSLog(@"%@",change); 23 NSLog(@"新的课程名称发生变化了,为:%@",[change objectForKey:@"new"]); 24 NSLog(@"旧的课程名称发生变化了,为:%@",[change objectForKey:@"old"]); 25 } 26 27 -(void)print 28 { 29 NSLog(@"name: %@ age: %d", name, age); 30 [course print];//代码复用 31 } 32 33 -(void)dealloc 34 { 35 //移除观察者 36 [course removeObserver:self forKeyPath:@"courseName"]; 37 [name release]; 38 [course release]; 39 [super dealloc]; 40 } 41 42 @end</font>
-----------------------Course.h-------------------------------
1 <font size="3">#import <Foundation/Foundation.h> 2 3 @interface Course : NSObject 4 { 5 NSString* courseName; 6 } 7 -(void)print; 8 @end</font>
-----------------------Course.m-------------------------------
1 <font size="3">#import "Course.h" 2 3 @implementation Course 4 5 -(void)print 6 { 7 NSLog(@"coursename%@",courseName); 8 } 9 10 -(void)dealloc 11 { 12 [courseName release]; 13 [super dealloc]; 14 } 15 16 @end 17 </font>
标签:
原文地址:http://www.cnblogs.com/cai1432452416/p/5322372.html