objective-C中的Class(类类型),Selector

2016-02-25  本文已影响0人  有来有趣ly

objective-C中的Class(类类型),Selector(选择器SEL),函数指针(IMP)

今天在园子里看到了一篇牛文“Objective-C 2.0 with Cocoa Foundation--- 5,Class类型,选择器Selector以及函数指针 ”,讲得十分精彩,忍不住把它的代码加上注释整理于此,以便日后查看。
个人体会:obj-C中的“Class类型变量”比c#中的Object基类还要灵活,可以用它生成任何类型的实例(但是它又不是NSObject)。而选择器SEL与函数指针IMP,如果非要跟c#扯上关系的话,这二个结合起来,就点类似c#中的反射+委托,可以根据一个方法名称字符串,直接调用方法。
"牛"的基类 Cattle.h
1
2
3
4
5
6
7
8

import <Foundation/Foundation.h>

@interface Cattle : NSObject {
int legsCount;
}

import "Cattle.h"

@implementation Cattle

-(void) saySomething
{
NSLog(@"Hello, I am a cattle, I have %d legs.", legsCount);
}

-(void) setLegsCount:(int) count
{
legsCount = count;
}
@end
子类“公牛" Bull.h
1
2
3
4
5
6
7
8
9
10

import <Foundation/Foundation.h>

import "Cattle.h"

@interface Bull : Cattle {
NSString *skinColor;
}

import "Bull.h"

@implementation Bull

-(void) saySomething
{
NSLog(@"Hello, I am a %@ bull, I have %d legs.", [self getSkinColor],legsCount);
}

-(NSString*) getSkinColor
{
return skinColor;
}

import <Foundation/Foundation.h>

//定义几个字符串常量

define SET_SKIN_COLOR @"setSkinColor:"

define BULL_CLASS @"Bull"

define CATTLE_CLASS @"Cattle"

@interface DoProxy : NSObject {
BOOL notFirstRun;

id cattle[3];
//定义二个选择器
SEL say;
SEL skin;
 
//定义一个函数指针(传统C语言的处理方式)
void(*setSkinColor_Func)(id,SEL,NSString*);
 
//定义一个IMP方式的函数指针(obj-C中推荐的方式)
IMP say_Func;
 
//定义一个类
Class bullClass;

}

-(void) doWithCattleId:(id) aCattle colorParam:(NSString*) color;
-(void) setAllIVars;
-(void) SELFuncs;
-(void) functionPointers;

@end
DoProxy.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

import "DoProxy.h"

import "Cattle.h"

import "Bull.h"

@implementation DoProxy

//初始化所有变量

//初始化id

//初始化选择器以及相应函数

//函数指针测试

@end
测试主函数main()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

import <Foundation/Foundation.h>

import "DoProxy.h"

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
DoProxy *doProxy = [DoProxy new];

[doProxy setAllIVars];
[doProxy SELFuncs];
[doProxy functionPointers];
 
[doProxy release];
[pool drain];
return 0;

}
运行结果:

2011-02-28 21:40:33.240 HelloSelector[630:a0f] Running in the method of doWithCattleId:colorParam:
2011-02-28 21:40:33.245 HelloSelector[630:a0f] Hi, I am a Cattle, have not setSkinColor!
2011-02-28 21:40:33.247 HelloSelector[630:a0f] Hello, I am a cattle, I have 4 legs.
2011-02-28 21:40:33.248 HelloSelector[630:a0f] Hello, I am a red bull, I have 4 legs.
2011-02-28 21:40:33.250 HelloSelector[630:a0f] Hello, I am a black bull, I have 4 legs.
2011-02-28 21:40:33.251 HelloSelector[630:a0f] Hi, you are a DoProxy, but I like cattle or bull!
2011-02-28 21:40:33.252 HelloSelector[630:a0f] Running as a function pointer will be more efficiency!
2011-02-28 21:40:33.254 HelloSelector[630:a0f] Hello, I am a verbose bull, I have 4 legs.

作者:菩提树下的杨过
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

上一篇下一篇

猜你喜欢

热点阅读