Tutorial

Objective-C Hello World Tutorial

Published on August 3, 2022
Default avatar

By Anupam Chugh

Objective-C Hello World Tutorial

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.

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

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 : objective-c-setup-wizard Choose Create a new XCode Project and follow the setup wizard given below : objective-c-choosing-template 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. In a few moments, you’ll see Xcode’s main interface like this: objective-c-main-interface As its visible in the above image the extension used for Objective-C programs is .m

Code

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;
}
  • @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 : objective-c-hello-world-output

  • 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.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Anupam Chugh

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
September 27, 2021

Pls Make menu how many languages you support , Like Swift, Objective c, Php and give link to that all .

- Ashwini

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    April 4, 2019

    hai bro, If it is easy to learn for a ios developer

    - Naveen

      Try DigitalOcean for free

      Click below to sign up and get $200 of credit to try our products over 60 days!

      Sign up

      Join the Tech Talk
      Success! Thank you! Please check your email for further details.

      Please complete your information!

      Get our biweekly newsletter

      Sign up for Infrastructure as a Newsletter.

      Hollie's Hub for Good

      Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

      Become a contributor

      Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

      Welcome to the developer cloud

      DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

      Learn more
      DigitalOcean Cloud Control Panel