一直想要做个ios的app,心动不如赶快行动,在网上找了个教程和教程2开始慢慢地一步一步地学习。
不知道能否争取一个星期看完基本概念。
加油吧!特写此篇作为纪念里程碑。
我的第一个objective-c 程序:
Program 1:
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
NSLog(@"Hello, World!");
NSLog(@"Hmmm this is my first Objective-C app, whooray!");
}
return 0;
}
或者
Program 2:
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSLog (@"hello world");
[pool drain];
return 0;
}
一行一行地讲解:
#import <Foundation/Foundation.h>
引入Foundation库。
int main(int argc, const char * argv[]) {}
main function indicates the beginning point of program execution and int is the return type of the main function. The statement inside the main function is used for command line argument and these parenthesis {} indicates the beginning and end of the main function.
主方法,写过java的人应该可以很容易明白,举一反三。
Program 1:
@autoreleasepool {}
Program 2:
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
[pool drain];
先说说这两句表达的意思,autoreleasepool,中文意思就是自动清除池。查看了一下教程2里的解释:
NSAutoreleasePool is a class used for memory management. It reserves a space in the memory for auto-release pool. Autorelease pools are created just like any other object, using alloc and init.
看来这个是用于内存管理。
为什么会有这两种写法,原因是program1是我用xcode自动生成的。我估计program1的写法是program2的新写法,这种写法大大简化了书写以及debug的流程。
NSLog(@"Hello, World!");
这句相当于php的echo,用于打印数据内容。
return 0;
这句结束main program,然后返回一个状态值0,意味着正常终止该程序。
今天记录到这里结束!