GNU/Linux Programmer's Solver - Objective-C Language

Hello World program in Objective-C. Basic structure, comments and string literals
PROGRAMMING OBJECTIVE-C HELLO WORLD
#import <Foundation/Foundation.h>

/* We can use multiline comments
   same way than in C */
int main () {
  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  
  NSLog (@"Hola Mundo\n"); // Also one line comments ala C++
  
  [pool drain];
  return 0;
}

Objective-C, like C++, is a superset of C and therefore, the code we have shown for the C version of our “Hello World” is perfectly valid. However, the language gained popularity when it was chosen as the programming language for the NeXT platform and its NeXTStep operating system. NeXTStep, like any operating system worth its salt, offered programmers a number of elements to facilitate application programming.

Let’s focus on the use of Objective-C on this platform for two fundamental reasons. The first is that otherwise, the Objective-C and C versions would be identical (unless we wanted to deliberately complicate things). The second is that much of NeXTStep still exists in Apple’s operating systems, as well as an open-source version known as GNUStep.

It is worth taking a look at NeXTStep due to the elegance (inherited from Smalltalk) both in its design and implementation, and that is why we include this facet in this book.

DID YOU KNOW

All those functions and classes begin with NS as they are part of the NeXTStep operating system.

The first and last line of the program make use of the NSAutoreleasePool class, which offers a reference counting memory management system. What this means, without going into detail, is that whenever we use an object, an internal usage counter is incremented. Likewise, when we stop using an object, the counter is decremented. However, the object in question will only be destroyed when that counter reaches zero. This allows us to avoid accidentally deleting objects that are still being used elsewhere in our program. The drain message at the end is responsible for releasing the objects in this environment.

Although, in this example, these operations are not necessary, it is common in Objective-C programs and that is why we include it in this “Hello World”.

I’M A NEWBIE

Objective-C programs use the @ character to access language-specific functions and differentiate them from standard C. For example, in our example program, by prefixing the at sign to the string of characters, we are telling the compiler that we want it to generate an object of the NSString class instead of a standard C string (a sequence of bytes at a specific memory location terminated with the character \0).

To be able to use Objective-C and the standard libraries, we must install the following packages:

gnustep-devel and gobjc

For Debian-based distributions.

$ apt install gnustep-devel gobjc

Objective-C programs typically use the .m extension. To compile the “Hello World” program, we must use a command like the following.

$ gcc -o helloGS hello.m `gnustep-config --objc-flags` \
> -L/usr/lib/GNUstep/ -lgnustep-base -lobjc

Objective-C was mainly created by Brad Cox and Tom Love in the early 1980s at their own company called Productivity Product International. Objective-C sought to combine the power of the Smalltalk language with the C compatibility that was very important at the time.

As with C and C++, Objective-C was initially described in a book titled Object-Oriented Programming, An Evolutionary Approach published in 1986.

It was in 1988 when NeXT licensed Objective-C from its creators. The company was now called StepStone. NeXT developed the AppKit and the Foundation Kit, something like the equivalent of the C standard library or the C++ STL, and used them to develop NeXTStep.

In 1996, Apple bought NeXT and used OpenStep (NeXTStep’s specification for using it on other systems) to develop Mac OSX, which included development tools for Objective-C. Eventually those tools converged into XCode, and much of the work developed by NeXT ended up in the Cocoa APIs.

Summary

  • Compiled language
  • Extension: m
  • Terminador de líneas: ;
  • Agrupa instrucciones con : {...}
  • Comentarios multilinea: Between characters /* */
  • Comentarios misma lina: //
  • Delimitador de cadenas: "cadena¨ or @"cadena"
  • Puedes imprimir textos con NSLog or puts (Does not add carriage return)
  • No soporta cadenas multilínea nativamente
  • No soporta HERE-DOCS
  • Punto de entrada: main
  • Requires definition of all elements beforehand (include files to declare functions)

Related Posts

GNU/Linux Programmer's Solver - C++ Language

Hello World program in C++. Basic structure, comments and string literals

Read

PROGRAMMING C++ HELLO WORLD

GNU/Linux Programmer's Solver
Chapter 1 - Hello World

Hello World is the first program you write when learning a new language and there are good reasons for that

Read

PROGRAMMING HELLO WORLD

GNU/Linux Programmer's Solver - Perl

Hello World program in Perl. Basic structure, comments and string literals

Read

PROGRAMMING PERL HELLO WORLD

Return to Home Page