Objective-C Hello World 教程

由于它已经是常规的开始一个编程课程与一个程序,在计算机屏幕上写字Hello world!我们将开始这个教程,开始与开发人员工具,然后沉浸在Hello World代码。

目标C的概述

Objective-C是用于为苹果 iOS 和 OS X 操作系统编写应用程序的编程语言。Objective-C 编程语言基于 C,但它增加了对象导向编程的支持。

安装 Apple 开发工具

编写 iOS 和 Mac 应用程序所需的主要应用程序是 Xcode. XCode 是 Apple 的 集成开发环境,仅在 Mac 上可下载。

开始使用 XCode

Xcode resides in the Applications folder and the following dialogs pop up when its launched : objective-c-setup-wizard Choose Create a new XCode Project and follow the setup wizard given below : objective-c-choosing-template In these tutorials we'll focus on the basic programming aspects and stay away from user interfaces to avoid complications, hence select Command Line Tools and click Next. Enter your respective organisation details and the Project Name. Choose the language as Objective-C as shown below and click next : objective-c-project-details In the next window, choose the folder in which you want your project directory to be created. Repository won't be needed for this project so uncheck the box labeled Create git repository. Finally, click the Create button. In a few moments, you’ll see Xcode’s main interface like this: objective-c-main-interface As its visible in the above image the extension used for Objective-C programs is .m

代码

**main 是程序首次启动时呼叫的函数的名称。

1#import <Foundation/Foundation.h>

当 Xcode 创建该项目时,它会导入 基础框架。框架是相关类、函数、常数和类型的组合。基础框架包含所有 iOS 应用程序和 OS X 应用程序中使用的基本类。 #import 比在 c 中使用的 #include 更快、更高效。当编译器看到 #include 指令时,它会对要包含的文件内容进行模糊的复制和粘贴。当编译器看到 #import 指令时,它首先会检查是否另一个文件可能已经导入或包含了该文件。

1#import <Foundation/Foundation.h>
2
3int main(int argc, const char * argv[]) {
4    @autoreleasepool {
5        // insert code here...
6        NSLog(@"Hello, World!");
7    }
8    return 0;
9}
  • @autoreleasepool创建了一个范围区域,使它更清晰,在池内是什么。内部的 @autoreleasepool块是我们写我们的代码
  • 接下来的行呼叫 NSLog,这是一个由基金会框架引入的函数。这个函数类似于在c中的printf()函数。它接受一个格式字符串,并且可以有可替代的代币。主要显而易见的区别是,NSLog会自动创建一个新行后一个字符串 *@是创建一个 NSString(我们将在以后讨论的基金会框架的另一个类别)的Objective-Chand,从给定的字符串 的对象

Build and run the program from the top left corner. The following output is shown in the console below : objective-c-hello-world-output

  • NSLog() 以日期、时间、程序名称和进程 ID 预先表示其输出 * Program exited with status value:0 - 这表示主 的返回值

这就结束了这个教程。

Published At
Categories with 技术
Tagged with
comments powered by Disqus