tags:

views:

25

answers:

1

Hello, I have the following class:

@interface Gamer {
...
}

+(id) CreatePlayer;
@end

@implementation Gamer

+(id) CreatePlayer
{
  return [[[self alloc] init]autorelease];
}
@end

I need to use the Gamer in an another class as instance variable. For example like this:

@interface Layer{
  Gamer * mCenterGamer;
}
@end
@implementation
-(void) init{
   mCenterGamer =  [Gamer CreatePlayer];
}
-(void) exampleFuncForUseGamer{
   [mCenterGamer ...]// some methods of the Gamer class
}
@end

Is it correct? (I think autorelease freed the mCenterGamer after exiting from the init function)

A: 

You need to retain mCenterGamer (and make sure to release it in the Layer's -dealloc method). Also, -init needs id as its return type:

- (id)init {
    if (self = [super init])
       mCenterGamer = [[Gamer CreatePlayer] retain];

    return self;
}

- (void)dealloc {
    [mCenterGamer release];
    [super dealloc];
}

Your -exampleFuncForUseGamer should be fine, depending on what you're trying to do there.

Wevah
Thanks for help
Romula