Objcetive-C 中处理共用变量和方法

可以为公用变量和方法创建公用类,也可以用公用文件,如下:

头文件 CommParam.h:

代码
 
   
1 // 标准方式定义enum
2   enum E1{
3 A1 = 1 ,A2
4 };
5
6 // 用typeof定义E2
7 typedef enum {
8 B1 = 10 ,B2
9 } E2;
10
11 // 定义公用变量
12 extern int count;
13
14 // 定义公用变量(其实是常量)
15 extern const int CALLNUM;
16
17 // 定义C语言方法(可以在其中用Objective-c的对象和方法)
18 extern void hello(NSString * name);

源文件 CommParam.m(文件名可以不是CommParam.m):

 
  
1 #import " CommParam.h "
2
3   // 为count赋初始值
4 int count = 1 ;
5
6 // 为CALLNUM赋初始值
7 const int CALLNUM = 10 ;
8
9 // 实现hello方法
10 void hello(NSString * name) {
11 NSLog( @" hello, %@ " , name);
12 }

测试文件 Test.m:

代码
 
   
1 #import < Foundation / Foundation.h >
2 #import " Fraction.h "
3 #import " Fraction2.h "
4 #import " CommFun.h "
5 #import " CommParam.h "
6   int gGlobalVar = 5 ;
7
8   int main ( int argc, const char * argv[]) {
9 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
10 hello( @" Tom " );
11 enum E1 e1 = A1;
12 E2 e2 = B1;
13 NSLog( @" this is E1 object value:%i " ,e1);
14 NSLog( @" this is E2 value:%i " ,e2);
15 NSLog( @" CALLNUM:%i " ,CALLNUM);
16 count ++ ;
17 NSLog( @" count:%i " ,count);
18 [pool drain];
19
20 return 0 ;
21 }

 

转载于:https://www.cnblogs.com/pengxl/archive/2010/12/28/1918800.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值