欢迎访问移动开发之家(rcyd.net),关注移动开发教程。移动开发之家  移动开发问答|  每日更新
页面位置 : > > > 内容正文

Objective-C初探-Objective-C HelloWorld,

来源: 开发者 投稿于  被查看 25001 次 评论:246

Objective-C初探-Objective-C HelloWorld,


上一篇博文,我已经就如何搭建和使用Xcode开发环境做过一番描述,按照惯例在正餐开始前先来个 Hello World
默认的.m文件中代码如下
01 #import <Foundation/Foundation.h>
02 
03 int main (int argc, const char * argv[]) {
04     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
05 
06     // insert code here...
07     NSLog(@"Hello, World!");
08     [pool drain];
09     return 0;
10 }

首先输入两个斜杠 (//) 做为注释。接着修改一些内容来显示范例 2-1 中的 Programming is fun!
01 // First program example
02 #import <Foundation/Foundation.h>
03 int main (int argc, const char * argv[])
04 {
05     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
06     NSLog(@"Programming is fun!");
07     
08     [pool drain];
09     return 0;
10 }

下面详细介绍下每条语句:
#import <Foundation/Foundation.h> 是告诉编译器找到并预处理名为 Foundation.h 的系统文件,import 在这里表示将文件的信息导入。
int main (int argc, const char * argv[]) main 用于准确地表示程序将于何处开始执行。int 是保留字制定了 main 函数返回值的类型为整型。圆括号的内容以后再细说。
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 建立内存自动释放池,而后面的 [pool drain]; 将已分配的内存释放。
NSLog(@"Programming is fun!"); NSLog 是一个例程,@"Programming is fun!" 是传说给它的参数。@ 符号在位于一对双引号的字符串签名。这称为常量 NSString 对象。
return 0; 返回一个状态值 0 给main函数。按照约定 0 意味着程序正常结束。
运行程序后,会在 Debugger Console 窗口里出现程序的输出。输出内容大致如下

[Session started at 2011-04-15 13:00:51 +0800.]

2011-04-15 xx:xx:xx.xxx 0201HelloWorld[1060:903] Programming is fun!


The Debugger has exited with status 0.


如果窗口没有自动出现,可进入主菜单栏并从 Run 菜单中选择 Console 即可。输出的 Programming is fun! 之前的文字与数字都是系统信息,最后一句表示 Debugger 调试器返回值为 0 状态为正常结束。以后的范例 Mike 就不再贴出这些信息了。
最后分享下笔者在CSDN上传的源代码链接:Obejctive-C HelloWorld

相关文章

    暂无相关文章

用户评论