C哩c哩舞,objective-c教程_Objective-C Hello世界教程

 2023-11-19 阅读 28 评论 0

摘要: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 Wo

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.

C哩c哩舞, 由于習慣于以寫有“ 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)

c哩c哩歌手。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

C.c,

Xcode駐留在Applications文件夾中,并在啟動時彈出以下對話框:

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

objective_c基礎都有啥。 選擇“創建新的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教程

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/182934.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息