objective-c教程_Objective-C Hello世界教程

objective-c教程

Since it has been conventional to begin a programming course with a program that writes the words “Hello world!” to the computer screen we’ll begin this tutorial by getting started with the Developer Tools and then dive into the Hello World code.

由于习惯于以写有“ Hello world!”字样的程序开始编程课程, 在计算机屏幕上,我们将从开始使用开发人员工具开始本教程,然后深入研究Hello World代码。

Objective-C概述 (Overview of Objective-C)

Objective-C is the programming language that is used to write applications for Apple’s iOS and OS X operating systems. The Objective-C programming language is based on C, but it adds support for object-oriented programming. All Objective-C programming is done with the Foundation framework.

Objective-C是用于为Apple的iOS和OS X操作系统编写应用程序的编程语言。 Objective-C编程语言基于C,但是它增加了对面向对象编程的支持。 所有的Objective-C编程都是通过Foundation框架完成的。

安装苹果的开发人员工具 (Installing Apple’s Developer Tools)

The main application that will be needed to write iOS and Mac Applications is Xcode. XCode is Apple’s Integrated Development Environment and is only available on Mac. It is downloadable from the App Store.

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

XCode入门 (Getting Started with XCode)

Xcode resides in the Applications folder and the following dialogs pop up when its launched :

objective-c-setup-wizard

Xcode驻留在Applications文件夹中,并在启动时弹出以下对话框:

Choose Create a new XCode Project and follow the setup wizard given below :

选择“创建新的XCode项目”,然后按照下面给出的设置向导进行操作:

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.

输入您各自的组织详细信息和项目名称。 选择语言作为Objective-C,如下所示,然后单击下一步:

在下一个窗口中,选择要在其中创建项目目录的文件夹。 此项目不需要存储库,因此请取消选中标有创建git存储库的框。 最后,单击创建按钮。

In a few moments, you’ll see Xcode’s main interface like this:

objective-c-main-interface

片刻之后,您将看到Xcode的主界面,如下所示:

As its visible in the above image the extension used for Objective-C programs is .m

如上图所示,用于Objective-C程序的扩展名为.m

(Code)

main is the name of the function that is called when a program first starts.

main是程序首次启动时调用的函数的名称。

#import <Foundation/Foundation.h>

This statement is written above the main function. When Xcode creates the project, it imports the Foundation framework. A framework is a set of related classes, functions, constants, and types. The Foundation framework contains fundamental classes that are used in all iOS apps and OS X applications.
#import is faster and more efficient as compared to #include used in c. When the compiler sees the #include directive, it makes a dumb copy-and-paste of the contents of the file to include. When the compiler sees the #import directive, it first checks to see if another file may have already imported or included the file.

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

The HelloWorld code is given below :

HelloWorld代码如下:

main.m

main.m

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        NSLog(@"Hello, World!");
    }
    return 0;
}
  • @autoreleasepool creates a scoped area and makes it clearer what’s within the pool. Inside of the @autoreleasepool block is where we write our code

    @autoreleasepool创建一个作用域区域,并使其更清楚池中的内容。 在@autoreleasepool块的内部,我们编写代码
  • The next line calls NSLog which is a function brought in by the Foundation Framework. This function is a lot like printf() function in c. It accepts a format string and can have replaceable tokens. The main noticeable difference is that NSLog automatically creates a newline after a string

    下一行调用NSLog ,这是Foundation Framework引入的功能。 这个函数很像c中的printf()函数。 它接受格式字符串,并且可以具有可替换的标记。 主要的明显区别是NSLog在字符串后自动创建换行符
  • “@” is an Objective-C shorthand for creating a NSString(Another class of Foundation framework that we will discuss later) object from the given character string

    “ @”是Objective-C的简写形式,用于根据给定的字符串创建NSString (另一类Foundation框架,我们将在后面讨论)。
  • return 0; : By convention, a return value of zero indicates that the function was successful

    return 0; :按照惯例,返回值为零表示函数成功

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() prefaces its output with the date, time, program name, and process ID

    NSLog()在其输出的开头加上日期,时间,程序名称和进程ID
  • Program exited with status value:0 – This is an indication of the return value of main

    Program exited with status value:0 -这表明主程序的返回值

This brings an end to this tutorial.

本教程到此结束。

翻译自: https://www.journaldev.com/9512/objective-c-hello-world-tutorial

objective-c教程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值