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.
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.
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.
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
main is 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 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. The HelloWorld code is given below : 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 stringreturn 0;
: By convention, a return value of zero indicates that the function was successfulBuild 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 mainThis brings an end to this tutorial.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Pls Make menu how many languages you support , Like Swift, Objective c, Php and give link to that all .
- Ashwini
hai bro, If it is easy to learn for a ios developer
- Naveen