Hello all,
I have only just started coding objective C, and I tried to code something simple: a class that stores a string and then retrieves it.
Code:
#import <Cocoa/Cocoa.h>
#import <stdio.h>
@interface Person: NSObject {
NSString *name;
}
@property (retain) NSString* name;
-(void) dealloc;
@end
@implementation Person
@synthesize name;
-(id) init
{
if(self = [super init])
{
[self setName:nil];
}
return self;
}
-(void) dealloc
{
printf("Calling dealloc\n");
self.name = nil;
[super dealloc];
}
@end
int main(int argc, char *argv[])
{
Person *person = [[Person alloc] init];
NSString *str = [[NSString alloc] initWithCString:"Test"];
person.name = str;
NSLog(@"%@", person.name);
[str release];
[person release];
}
However, when running it it reports "autoreleased with no pool in place". Why? It seems to be in the synthesized name setter. From the tutorial I read, this should be fine - do I always need an autorelease pool if I use "@synthesize"? Where should I make it - it's probably not good to have it always in main, nor is it very useful to set up and releasing a pool every time I use a setter? [edit: I expected that the autoreleased objects would only be released after the autorelease pool is released, but maybe this is not true?]
Also, what does Objective C say about the default values of the members? Right now I set it to nil manually; do I need to, or is that standard? Also, do I need [super init] here, and do I need it if I don't need an "init" function, or does the default init function call [super init]?
Okay, maybe I should get myself an Objective C book, as the tutorials were dreadful (actually, one seemed quite good, but way too short)...
Thanks in advance