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 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框架完成的。
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 resides in the Applications folder and the following dialogs pop up when its launched :
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,如下所示,然后單擊下一步:
在下一個窗口中,選擇要在其中創建項目目錄的文件夾。 此項目不需要存儲庫,因此請取消選中標有創建git存儲庫的框。 最后,單擊創建按鈕。
In a few moments, you’ll see Xcode’s main interface like this:
As its visible in the above image the extension used for Objective-C programs is .m
如上圖所示,用于Objective-C程序的擴展名為.m
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;
}
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在字符串后自動創建換行符 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 :
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教程
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态