Archive for November 4th, 2007

Accessing Cocoa from Carbon from Cocoa

Sunday, November 4th, 2007

The title of this blog may take a little explaining.

I was looking to include a Carbon routine within my Cocoa application, but this routine needed to have access to both my instance variables and other Class methods. It is straight forward to use Cocoa commands in your Carbon routine, as long as they’re declared from within the routine itself. You have no access to the object reference “self”. This refers to the object in which your code resides.

In order to achieve this you first need to create an object of the Class method and an instance init method within your Class. Here I have a Class called AppController where my Carbon routine is located. I add the following code;

+ (AppController *) sharedController
{
    return sharedController;
}

- (id) init
{
    self = [super init];
    sharedController = self;

    // Any other initialiser code needed

    return self;
}

Now from within my Carbon routine a new AppController ojbect class is created called controller. I can now use this controller object in place of “self”. This gives me access to all my instance variables and methods.

void MyCarbonRoutine
{
    AppController *controller;
    controller = [[AppController alloc] init];
    [controller aCocoaRoutine];

    // Any other coding needed here

    [controller release];
}

Remember to release your controller object before leaving the Carbon routine or you could end up with all sort of problems.

- Thanks to Uli Kusterer for pointing out that they are Objects that are created and released and not the Classes themselves.

WordPress & Apple Server

Sunday, November 4th, 2007

For those that haven’t noticed, this blog is using Wordpress. This is freely available for download from their website. If like me your using it on a Macintosh server and want to use a feature like Permalink, you may be having problems. The Wordpress documentation describes these as:

Permalinks are the permanent URLs to your individual weblog posts, as well as categories and other lists of weblog postings. A permalink is what another weblogger will use to refer to your article (or section), or how you might send a link to your story in an e-mail message. Especially when they are used to link to individual postings, once a story is posted, the URL to it should be permanent, and never change. Hence.. permalink.

This is a really nice feature but the Apache server settings on the Mac are overridden. In order for Permalinks to work you need an “.htacces” file which is a “distributed configuration file”. This provides a way to make configuration changes in each directory. A file, containing one or more configuration directives, is placed in a particular document directory, and the directives apply to that directory, and all subdirectories thereof. This is needed for these fancy links to work but the Mac Server overrides them.

To fix this problem simply go to the Web option in your ServerAdmin application. Select the website that’s using Wordpress under the Sites toolbar and select Options.

Apache options

Under the Apache options list, ensure that “Allow All Overrides” tick box is checked. This will allow Apache to use your “.htaccess” files and the Permalinks will run as they should.