Objective-C for Java programmers

This post is a quick reference guide for Java programmers new to Objective-C programming on the Mac or iPhone. I will continue to post updates here as I find out more myself. I make no claims as to their accuracy - just my observations along my own journey of discovery...


Enum's in Objective-C are quite simplistic in comparison with Java. Here is an example from NSProcessInfo.h. So Solaris would be 3.

enum { /* Constants returned by -operatingSystem */
NSWindowsNTOperatingSystem = 1,

But when you see the definition of the operatingSystem function, it actually returns integer (rather than the enum)!

- (NSUInteger)operatingSystem;

You could:

typedef enum { ... } OS;
- (OS)operatingSystem;

but as it all evaluates to int in the end no types will be checked and so it will be no help to you anyway!



See also categories & posing. Seriously! :-)


I cannot find an equivalent for this, which is very annoying as you never know if you have overridden a method or simply put a new one in your class with a similar signature. Xcode is a co-conspirator in this plot as it doesn't mark methods which override others, as Eclipse does (in the margin).


There could be a whole other blog on this. Now investigating HeaderDoc (which is built-in) and Doxygen, which is third-party but seems to have Apple approval.


Still looking...


#include "Verbose.h"

void oneway() {
int i = 12;
NSLog(@"oneway called");
[NSException raise:@"Example exception" format:@"i was %d", i];

void anotherway() {
NSLog(@"anotherway called");
NSException *exception = [NSException exceptionWithName:@"foo" reason:@"bar" userInfo:nil];
@throw exception;

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

// exception example...
@try {
// dealloc is called on objects on the stack on a clean-exit
Verbose *v = [[[Verbose alloc] init] autorelease];
// No way to @suppress this unused variable warning
// Can turn off warnings at the compiler level but not a good idea
@catch(NSException *ex) {
// No way to get a useful stack trace out - just numbered frames
NSLog(@"exception raised '%@'", ex);
anotherway(); // comment this out for a clean exit
@catch(NSException *ex) {
// No warning/error/effect for the same argument name
NSLog(@"Can't catch another '%@'", ex);
@finally {
NSLog(@"clean up");

[pool drain];
return 0;

init called
oneway called
exception raised 'i was 12'
anotherway called
clean up
*** Terminating app due to uncaught exception 'foo', reason: 'bar'
Stack: (


int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

// id is like (void*) in C++ or Object in Java
//id something = nil; // Pointers not set to nil by default
id something = [[NSArray alloc] init];
if ((something != nil) && [something isKindOfClass:[NSArray class]]) {
// Without the !=nil check the program will SEGV with something==nil
NSLog(@"It's an array!");

[pool drain];
return 0;

It's an array!