Getting started with Objective C

In order to do i{Phone|Pod|Pad} or Mac development you need to learn Objective-C, which is basically adding some object-orientation to the C language.

The last 2.5 days I spent some time in learning about Objective C. I used Xcode (an IDE for the Mac) and also a pretty straightforward (or minimalistic) enviroment:

  • text editor
  • compiler

All you really need for coding some Objective-C is a text-editor and a compiler. On my Mac I used TextMate and the gcc compiler. Sure, with Xcode iOS/Objective-C development is nicer, but a with a text editor and a compiler you get something in a short amount of time as well. (Note: Xcode is only available for Mac OS)

Using the gcc compiler I noticed a few issues, when actually compiling the bits, since I was never doing any C/C++ development before. The main reason for writing this blog is giving a quick introduction that also includes the required gcc commands…. – as there are already a lot of “Hello World”-ish blogs.
For professional iOS development I do recommend using Xcode, but let’s get started…

The canonical “Hello World” is simple and looks like:

#import <Foundation/Foundation.h>

int main(void) {
	NSLog(@"Hello Objective-C");

	return 0;
}

The NSLog() function requires a NSString object. The String starts with an @, this identifies the string as a NSString object… Save the above as hello.m and compile it:

gcc hello.m -o prog -framework Foundation

It is important to specify that you are using the Foundation classes (-framework Froundation)…
Now you can invoke your first program:

./prog

Now let’s take a look at a simple class. For that we need two files: a header file and it’s implementation (or method) file. Let’s take a look at the MyPerson.h file:

#import <Foundation/Foundation.h>

@interface MyPerson : NSObject {
}

// getter and setter
@property (nonatomic, copy) NSString* firstName;
@property (nonatomic, copy) NSString* secondName;

// should print the complete name
- (void) printDetails;

@end

The MyPerson class extends the NSObject class and it defines a few setter/getter, by using Objective-C’s property syntax. I also has another method to print out the name of an instance of this class. The MyPerson.m file looks like:

// usage of our header file
#import "MyPerson.h"

@implementation MyPerson

// getter/setter
@synthesize firstName = firstName_ ;
@synthesize secondName = secondName_ ;

// the actual method
- (void) printDetails {

    // write out the name, in a formated way:
    NSLog(@"%@, %@", self.secondName, self.firstName);
}

@end

In here we basically implement the defined methods, like the getter/setter or the printDetails.

Now we want to use the MyPerson class, which is done in the main.m file:

#import <Foundation/Foundation.h>
#import "MyPerson.h"

int main(void) {
	// memory mgmt class
	NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

    // autorelease to avoid leaks
    MyPerson* matze = [[[MyPerson alloc] init]autorelease];

    matze.firstName =  @"Matthias";
    matze.secondName = @"Wessendorf";

    [matze printDetails];

    // free the memory
	[pool release];
	return 0;
}

The main.m file imports our MyPerson class and its main() function creates an object of the class and sets values to the instance by using the two setter functions. Afterwards the printDetails is called to print the first and second name to the console. It also uses the NSAutoreleasePool class, for some memory management… The compilation is done with the following command:

gcc main.m MyPerson.m -o prog2 -framework Foundation

You can now invoke the prog2!

Thanks to Torsten Curdt! He was around when I had some newbie questions. Thanks!

Howdy!

Posted in Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: