Deepening my understanding of programming…
Mar/092
I’ve been programming for a long time; 11 years. This is just over half of my current age! I can confidently program in 4 different languages (and several markup languages) as well as get by in about 8 others.
One languages I began using 5 years ago, Objective-C is the only language not to follow the same trend as the other languages whilst learning it. When learning Objective-C, most programmers will be tying and making themselves almost dependent on Cocoa. This is not necessarily a bad thing. Cocoa is an excellent Framework, one which I very much enjoy having in my toolbox, but there are sometimes being able to use Objective-C without Cocoa may be advantageous.
I’ve recently run in to one such thing. I small experimental utility I’ve made for myself needs to run on Windows, Mac OS X and Linux. I fully had in mind that I was going to be using C# on Windows, C++ on Linux and Objective-C on the Mac. I know I could use C++ on all 3, but C++ is not my strong suit. I know it, and can program it well enough, but prefer not to.
Well, whilst visiting the website for GCC in order to download it to my Windows setup, I noticed the Objective-C compiler in it. I knew it was there, but I just never even considered Objective-C outside of the Mac. Seeing that I decided to try and create a small application with Objective-C.
I should stress that although stripping out Cocoa from Objective-C is a relatively painless task, there are some differences to basic method names and a lot of functionality that we take for granted is not there. For instance you a stuck at Text only applications unless you want to code a GUI from the ground up.
I set about creating a basic application that would display the infamous Hello World to the screen, but do it in a slightly overkill way… it would create a new class to print to the screen.
Here is the code for the header and implementation files for the class.
hello.h
#import <stdio.h>
#import <objc/Object.h>
@interface Hello : Object {
const char * message;
}
- (id)initWithMessage:(const char *)m;
- (void)setMessage:(const char *)m;
- (void)displayMessage;
- (void)free;
@end
hello.m
#import "hello.h"
@implementation Hello
- (id)initWithMessage:(const char *)m {
if (self = [super init]) {
message = m;
}
return self;
}
- (void)setMessage:(const char *)m {
message = m;
}
- (void)displayMessage {
printf("%s\n", message);
}
- (void)free {
[super free];
}
@end
main.m
#import <stdio.h>
#import "hello.h"
int main (void) {
Hello *hello = [[Hello alloc] initWithMessage:"Hello World"];
[hello displayMessage];
[hello free];
return 0;
}
Notice the lack of any NS naming? This example has used Objective-C, but has ditched Foundation and Cocoa. You may even notice the lack of -dealloc, which has been replaced by -free.
There are a few things to watch out for however. On Mac OS X plain Objective-C with out Cocoa has been severely crippled, due to the absence of some key classes. Namely NXConstantString. This is the class in Objective-C that allows for the @”" style string creation. Apple has replaced the class inside of Cocoa and Foundation to use NSConstantString, and the developer tools tell GCC to look for this class rather than NXConstantString. You should (I have not tested this yet) be able to use NXConstantString on Windows and Linux.
This is draw back on the Mac, but we do have Cocoa and Foundation in its place. NSConstantString is a much better version of NXConstantString, as it descends from NSString which includes all the encoding information for localizing applications.
Objective-C itself is a very small thing. Its got may be a dozen or so header files inside GCC. Lets put this in perspective. Cocoa and Foundation combined have about several hundred header files. This is what most Objective-C programmers are learning. Its Cocoa and Foundation. Yes they know the Objective-C syntax and can program it, but the majority will never program plain Objective-C.
I find this to be rather interesting. For all other languages we learn the actual languages, but with Objective-C we spend a tiny amount of time learning the syntax and the rest learning the frameworks. Perhaps this is why its seen to be such a difficult thing to learn, because of the sheer overwhelming size of it. If people learnt Objective-C with out Cocoa to begin with, and then eased in things like Foundation, perhaps it might be that bit easier for new comers.
Then again I could have just been spouting crap :). Anyone got any experience programming plain Objective-C, or have any thoughts on my thoughts?
Leave a comment
No trackbacks yet.
2:23 am on September 1st, 2009
usarlo lo mejor posible
2:25 pm on September 22nd, 2009
Hey, you may also want to check gnustep, as an cross-platform objc environment. You can use the foundation classes under Windows and Unix. Pretty cool to make comand-line tools on the 3 environments.
You can even make UI apps, but I wouldn’t recommend it…