Objective-C Programming Online Tutorial

Start-up Development Team In Their Office.
vgajic/Getty Images

This is the part of a series of tutorials on Programming in Objective-C. It's not about iOS development though that will come with time. Initially, though, these tutorials will teach the Objective-C language. You can run them using ideone.com.

Eventually, we'll want to go a bit further than this, compiling and testing Objective-C on Windows and I'm looking at GNUStep or using Xcode on Macx.

    Before we can learn to write code for the iPhone, we really need to learn the Objective-C language. Although I'd written a developing for iPhone tutorial before, I realized that the language could be a stumbling block.

    Also, memory management and compiler technology have changed dramatically since iOS 5, so this is a restart.

    To C or C++ developers, Objective-C can look quite odd with its message sending syntax [likethis] so, a grounding in a few tutorials on the language will get us moving in the right direction.

    What Is Objective-C?

    Developed over 30 years ago, Objective-C was backwards compatible with C but incorporated elements of the programming language Smalltalk.

    In 1988 Steve Jobs founded NeXT and they licensed Objective-C. NeXT was acquired by Apple in 1996 and it was used to build the Mac OS X Operating System and eventually iOS on iPhones and iPads.

    Objective-C is a thin layer on top of C and retains backward compatibility such that Objective-C compilers can compile C programs.

    Installing GNUStep on Windows

    These instructions came from this StackOverflow post. They explain how to install GNUStep for Windows.

    GNUStep is a MinGW derivative that lets you install a free and open version of the Cocoa APIs and tools on many platforms. These instructions are for Windows and will let you compile Objective-C programs and run them under Windows.

    From the Windows Installer page, go to the FTP site or HTTP Access and download the latest version of the three GNUStep installers for the MSYS System, Core, and Devel. I downloaded gnustep-msys-system-0.30.0-setup.exe, gnustep-core-0.31.0-setup.exe and gnustep-devel-1.4.0-setup.exe. I then installed them in that order, system, core and devel.

    Having installed those, I ran a command line by clicking start, then clicking run and typing cmd and pressing enter. Type gcc -v and you should see several lines of text about the compiler ending in gcc version 4.6.1 (GCC) or similar.

    If you don't, ie it says File not found then you may have another gcc already installed and need to correct the Path. Type in set at the cmd line and you'll see lots of environment variables. Look for Path= and many lines of text which should end in ;C:\GNUstep\bin;C:\GNUstep\GNUstep\System\Tools.

    If it doesn't, then open the Windows Control Panel look for System and when a Window opens, click Advanced System Settings then click the Environment variables. Scroll down the System Variables list on the Advanced tab until you find Path. Click Edit and select All on the Variable Value and paste it into Wordpad.

    Now edit the paths so you add the bin folder path then select all and paste it back into the Variable value then close all the windows.

    Press ok, open a new cmd line and now gcc -v should work.

    Mac Users

    You should sign up to the free Apple development programs and then download Xcode. There's a bit of setting up a Project in that but once it's done (I'll cover that in a separate tutorial), you will be able to compile and run Objective-C code. For now, the Ideone.com website provides the easiest method of all for doing that.

    What's Different about Objective-C?

    About the shortest program you can run is this:

    #import <Foundation/Foundation.h>

    int main (int argc, const char *argv[])
        NSLog (@"Hello World") ;
        return (0) ;

    You can run this on Ideone.com. The output is (unsurprisingly) Hello World, though it will be sent to stderr as that's what NSLOG does.

    Some Points

    • #import is the Objective-C equivalent of #include in C.
    • Instead of zero terminated C string, I've used Objective-C's strings. These always start with @ as in @"Example of a string".
    • The main function is no different.

    In the next Objective-C tutorial I'll look at objects and OOP in Objective-C.

    mla apa chicago
    Your Citation
    Bolton, David. "Objective-C Programming Online Tutorial." ThoughtCo, Mar. 26, 2017, thoughtco.com/objective-c-programming-online-tutorial-one-958338. Bolton, David. (2017, March 26). Objective-C Programming Online Tutorial. Retrieved from https://www.thoughtco.com/objective-c-programming-online-tutorial-one-958338 Bolton, David. "Objective-C Programming Online Tutorial." ThoughtCo. https://www.thoughtco.com/objective-c-programming-online-tutorial-one-958338 (accessed October 18, 2017).