Hello World in Objective-C

Hello World in Objective-C Featured Image

Hello again! In this seventh edition of the Hello World in Every Language series, we’re tackling Hello World in Objective-C.

Last time, I mentioned that we already covered the bulk of the languages I’m familiar with, so be prepared for me to enter some uncharted waters. I’ve never used Objective-C, so this should be interesting.

Table of Contents

Objective-C Background

In order to get familiar with Objective-C, I’ve leveraged WikipediaOpens in a new tab. as usual.

According to Wikipedia, Objective-C is another general-purpose language. In fact, it’s apparently quite similar to C, but it includes messaging akin to another language known as Smalltalk. Maybe we’ll create a Hello World article for that language as well.

Objective-C first appeared back in 1984 as the primary language for the NeXT and NeXTSTEP operating systems. From those operating systems came Apple’s OS X and iOS operating systems which both leveraged Objective-C.

Today, Objective-C is slowly being phased out by its successor, Swift. Or, at least that’s Apple’s goal. We’ll take a look at Swift in the next tutorial.

Hello World in Objective-C

So, it turns out that implementing Hello World in Objective-C is a bit complicated. As a result, I’ve decided to share a couple examples. Up first, the C-like example:

#import <Foundation/Foundation.h>

int main()
{
   NSLog(@"Hello, World!");
   return 0;
}

This should look very similar to our tutorial on Hello World in C, so I won’t bother going into it. However, I will note that the string style is a little different than we’re used to. We’ll notice an @ symbol just before the string. Apparently, that indicates to the compiler that we’re dealing with an NSString rather than a C string.

Anyway, let’s try an example which uses some of the more interesting features of Objective-C:

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   NSLog (@"Hello, World!");
   [pool drain];    
   return 0; 
} 

This example of Hello World in Objective-C is almost identical, but there are a couple new lines (5 & 7). Line 5 creates an NSAutoreleasepool which is effectively a scoping mechanism for objects. At line 7, all objects created within the scope of the pool are sent a release message. In a reference-counted environment, this decrements each objects reference counter which brings those objects one step closer to deletion.

The first example doesn’t actually require an NSAutoreleasepool. However, it’s an important feature to grasp when writing code in Objective-C.

How to Run the Solution

At any rate, we can try the code snippets above with this online Objective-C compilerOpens in a new tab.. As a warning, I was unable to get the first example working without switching to GCC 5.3.0. You’ll get a lot of autorelease errors otherwise.

Alternatively, if you’re a Mac user, you can leverage XcodeOpens in a new tab.. This same environment will allow you to code in Swift as well.

Sample Programs in Every Language

This tutorial on Hello World in Objective-C was actually quite a challenge for me. I’m unfamiliar with message passing which is fundamental to Objective-C. As a result, I had to do quite a bit of reading just to share these snippets above.

To follow-up Objective-C, we’re going to look into Apple’s upgrade called Swift. Hopefully, Swift will be easier to navigate. Objective-C has a lot of the ugly C syntax mixed with message passing. It’s definitely not the most readable language, but then again I’ve never been a fan of memory management.

Sample Programs in Every Language (44 Articles)—Series Navigation

For 100 Days of Code, I’ve decided to implement a few sample programs in as many languages as possible. Each implementation details a brief history of the language and a description of the code.

The plan for the series is to explore the major general-purpose language like Java, Python, C, C++, and C#. From there, we’ll take a look at some sample programs in web development languages like Ruby, PHP, and JavaScript. As we continue, we’ll cover proprietary languages like Swift and Objective-C. Eventually, we’ll start to tackle less popular languages like Rust, x86, and Verilog. Finally, we’ll play around with some of the esoteric languages like Brainf*ck and LOLCODE.

Who knows? Maybe the Sample Programs in Every Language series will become so popular it’ll never end. To help this series grow, consider sharing it on social media with your friends. Or, if you have a language you want to see, drop your suggestion in the comments.

Jeremy Grifski

Jeremy grew up in a small town where he enjoyed playing soccer and video games, practicing taekwondo, and trading Pokémon cards. Once out of the nest, he pursued a Bachelors in Computer Engineering with a minor in Game Design. After college, he spent about two years writing software for a major engineering company. Then, he earned a master's in Computer Science and Engineering. Today, he pursues a PhD in Engineering Education in order to ultimately land a teaching gig. In his spare time, Jeremy enjoys spending time with his wife and kid, playing Overwatch 2, Lethal Company, and Baldur's Gate 3, reading manga, watching Penguins hockey, and traveling the world.

Recent Posts