Disabling the ARC in Xcode 4.2

With Xcode 4.2 (iOS 5) Apple introduce ARC (Automatic Reference Counting) to help with memory management. Basically you no longer need to call retain or release. However current examples/books (and (open source) libraries) are still doing it. This leads to (annoying) errors like:

ARC forbids explicit message send of ‘retain’

To get rid of this error you need to disable ARC, but how? I found this link which show how to disable it for cetain source files, but I wanted this to be a global setting.

My Kaazing colleague Richard Clark told me how to turn off ARC at the project level. Here is what you need to do in Xcode:

  1. Select your project file.
  2. You should be in the “Build settings” tab. Select the “levels” option (default is “Combined”)
  3. There’s a search field to the right of “Combined”. Enter “Automatic”
  4. Second group should be “Apple LLVM Compiler 3.0 – Language”. Second line under that controls ARC.
  5. Click that line, then in the middle column (where it says “yes”), click and choose “no”.
Now the error is gone. Great!
Thanks Richard :-)

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!