2 - (void) setAllIVars;
3 - (void) SELFuncs;
4 - (void) functionPointers;
2 #import "Cattle.h"
3 #import "Bull.h"
4
5 @implementation DoProxy
6 - (void) setAllIVars
7 {
8 cattle[0] = [Cattle new];
9
10 bullClass = NSClassFromString(BULL_CLASS);
11 cattle[1] = [bullClass new];
12 cattle[2] = [bullClass new];
13
14 say = @selector(saySomething);
15 skin = NSSelectorFromString(SET_SKIN_COLOR);
16 }
17 - (void) SELFuncs
18 {
19 [self doWithCattleId:cattle[0] colorparem:@"brown"];
20 [self doWithCattleId:cattle[1] colorparem:@"red"];
21 [self doWithCattleId:cattle[2] colorparem:@"black"];
22 [self doWithCattleId:self colorparem:@"haha"];
23 }
24 - (void) functionPointers
25 {
26 setSkinColor_Func=(void (*)(id, SEL, NSString*)) [cattle[1] methodForSelector:skin];
27 //IMP setSkinColor_Func = [cattle[1] methodForSelector:skin];
28 say_Func = [cattle[1] methodForSelector:say];
29 setSkinColor_Func(cattle[1],skin,@"verbose");
30 NSLog(@"Running as a function pointer will be more efficiency!");
31 say_Func(cattle[1],say);
32 }
33 - (void) doWithCattleId:(id) aCattle colorparem:(NSString*) color
34 {
35 if(notFirstRun == NO)
36 {
37 NSString *myName = NSStringFromSelector(_cmd);
38 NSLog(@"Running in the method of %@", myName);
39 notFirstRun = YES;
40 }
41
42 NSString *cattleparemClassName = [aCattle className];
43 if([cattleparemClassName isEqualToString:BULL_CLASS] ||
44 [cattleparemClassName isEqualToString:CATTLE_CLASS])
45 {
46 [aCattle setLegsCount:4];
47 if([aCattle respondsToSelector:skin])
48 {
49 [aCattle performSelector:skin withobjecs:color];
50 }
51 else
52 {
53 NSLog(@"Hi, I am a %@, have not setSkinColor!", cattleparemClassName);
54 }
55 [aCattle performSelector:say];
56 }
57 else
58 {
59 NSString *yourClassName = [aCattle className];
60 NSLog(@"Hi, you are a %@, but I like cattle or bull!", yourClassName);
61 }
62 }
63 @end