Objective-C Hello World Tutorial

来源:互联网 时间:1970-01-01

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.

Overview of Objective-C

Objective-Cis the programming language that is used to write applications for Apple’s iOS and OSX 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.

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.

Getting Started with XCode

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

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

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 :

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:

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

Code

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

#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 OSX 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.

The HelloWorld code is given below :

#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 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 “@” 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 return 0; : By convention, a return value of zero indicates that the function was successful

Build and run the program from the top left corner. The following output is shown in the console below :

NSLog() prefaces its output with the date, time, program name, and process ID Program exited with status value:0 – This is an indication of the return value of main

This brings an end to this tutorial.



相关阅读:
Top