Objective-C 2.0 筆記 (1) 第一支 Objective-C 程式

系统需求:

至少要有一台 Mac 电脑,Mac OS X 10.6.3,并且已经安装 iPhone SDK(含 Xcode)。

步骤:

(1) 执行 Xcode,选择新建一个专案,程式类型为命令列使用 Foundation 的工具,如下图所示

1

2

(2) 我们就叫这个专案,HelloWorld,叫其他名字随你。

3

(4) Xcode 就已经帮你把样板程式生出来了。

4

(5) 接下来我们来为我们的程式,加上一个 Objective-C 的 class,看起来就有模有样了。

  1: #import 
  2: 
  3: //---- interface section ----
  4: 
  5: @interface HelloWorld : NSObject
  6: {
  7: 	int version;
  8: }
  9: 
 10: -(void) setVersion: (int) ver;
 11: -(int) version;
 12: -(void) print;
 13: 
 14: @end
 15: 
 16: //---- implementation section ----
 17: 
 18: @implementation HelloWorld
 19: 
 20: -(void) setVersion: (int) ver
 21: {
 22: 	version = ver;
 23: }
 24: 
 25: -(int) version
 26: {
 27: 	return version;
 28: }
 29: 
 30: -(void) print
 31: {
 32: 	NSLog(@"Hello World, ver %i", version);
 33: }
 34: 
 35: @end
 36: 
 37: //---- program section ----
 38: 
 39: int main (int argc, const char * argv[]) 
 40: {
 41:     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 42: 
 43: 	HelloWorld *myHelloWorld;
 44: 	
 45: 	myHelloWorld = [[HelloWorld alloc] init];
 46: 	
 47: 	[myHelloWorld setVersion: 1];
 48: 	[myHelloWorld print];
 49: 	
 50: 
 51: 	[myHelloWorld release];
 52: 	
 53:     [pool drain];
 54:     return 0;
 55: }
 56: 

(6) 选择编译并执行,然后把 Debug Console 叫出来,可以看到下面的结果。

5

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值