PDA

View Full Version : introduction to objective c


evan
2010-05-03, 01:04
here are some very simple classes I'm trying to get working just so I can move on from there to more complicated things:

listtest.m

#import <stdio.h>
#import "lst.h"

int main (void) {
Lst *l = [Lst new];
[l x: 10];
printf("%i\n", [l x]);
return 0;
}


lst.h

#import <objc/Object.h>

@interface Lst : Object
{
@private
int x;
}
- (id) x: (int) x_value;
- (int) x;
@end


lst.m

#import "lst.h"

@implementation Lst

- (id) x: (int) x_value {

x = x_value;
return self;
}

- (int) x {
return x;
}

@end


and here's what happens:

Evan-Daviss-iMac:code Evan$ gcc *.m -lobjc
listtest.m: In function ‘main’:
listtest.m:10: warning: ‘Lst’ may not respond to ‘+new’
listtest.m:10: warning: (Messages without a matching method signature
listtest.m:10: warning: will be assumed to return ‘id’ and accept
listtest.m:10: warning: ‘...’ as arguments.)


this code very closely resembles the tutorial I'm following which also does not have an init method, so i'm being led to believe it's not entirely necessary... and even when I try to add one it doesn't work, and gives the same warning with alloc and init that it does with new in this example. ugh, why is this so difficult!!

Majost
2010-05-03, 01:27
Wow, what tutorial are you following? It looks like it might be a Linux/GNU tutorial. The base Object class really doesn't do much by default... it doesn't even retain/release.

I would strongly recommend a tutorial that works with Apple's implementation of NSObject, especially if you want to eventually make an OS X or iPhone app. The easy solution is to #import <Cocoa/Cocoa.h> instead of objc/Object.h, and then have your Lst class extend NSObject instead of Object.

And then give up on that tutorial. There are a bunch of great Apple-style Objective-C tutorials around on the web. cocoadevcentral.com and macresearch.org both are good places to start. I'm sure there's more, too.

Edit: Oh, I figured it out. Apple ripped the guts out of the Object implementation in Objective-C 2.0. If gcc is compiling for Objective-C 2, Object will only respond to +class and -isEqual:. I don't know if there's a way to go back to the 1.x implementation.

Wyatt
2010-05-03, 06:50
Apple's documentation has a lot of good stuff, too, including some getting started guides for Mac, iPhone and Objective-C. I just started working through an iPhone development class from Stanford on iTunes U, and I'm enjoying that so far.

evan
2010-05-03, 09:30
http://en.wikibooks.org/wiki/Objective-C_Programming/syntax

Unfortunately this is for a class so when they run the final assignment submission it will be in Linux so I can't really jump ship and use apple's specialized obj c 2.0 stuff :(

Thanks for the info about the gcc compiler I'll bring that up in class today

Majost
2010-05-03, 10:28
Aha! Got it!

You simply need to use the old 4.0 version of gcc.

$ gcc-4.0 *.m -lobjc

evan
2010-05-03, 11:29
I love when it's a one line fix :)

why would apple take out all the Object functionality in their update to Obj-c? Seems an odd thing to do

Majost
2010-05-03, 13:12
Yeah, I was curious about that, too. I tried searching, but searching on 'Object' sucks. One very nice side-effect of no namespaces: NSObject is a beautiful search term.