初学Objective - C 第一天

本文记录了一个iOS应用开发新手从零开始的学习旅程,分享了第一个Objective-C程序的编写过程及理解,包括程序结构、内存管理机制等核心概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一直想要做个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,意味着正常终止该程序。

今天记录到这里结束!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值