0

lets imagine this scenario:

Game.h:

@interface Game : CCLayer
{    
    NSMutableArray* questions;
}

@property (nonatomic,retain) NSMutableArray* questions;


- (void) didLoadFromCCB;
- (void) pressitem:(id)sender;

@end

Game.m

 @implementation Game

 @synthesize questions;

 - (void) didLoadFromCCB
 {
     NSMutableArray *questions = [[NSMutableArray alloc] initWithObjects:[NSNumber numberWithInteger:-1],nil];

     NSLog(@"didload %@", questions);
 }


 - (void) pressitem:(id)sender
 {
     NSLog(@"pressitem %@",questions);
 }
@end

I get the log from didLoadFromCCB but on the pressitem it returns null. Shouldn't the array be accessible through all my implementation?

I know this seams like a really noob question, but i come from an actionscript/php background, and i just ordered a C and an Objective C book, but while i wait i just wanted to dig in a little.

thank you for your time :)

2 Answers 2

2

Your local declaration of questions in didLoadFromCCB shadows the instance variable. You should probably just make that line:

self.questions = [[NSMutableArray alloc] initWithObjects:[NSNumber numberWithInteger:-1],nil];

Then you will be creating the array and storing a pointer to it in the instance variable, rather than just making a local pointer that immediately goes out of scope.

Sign up to request clarification or add additional context in comments.

10 Comments

Why not set the property, if it's declared?
@Filip See Chuck's explanation.
@Chuck I completely lost the hope in iOS programmers, as such I don't anymore assume that one uses MRC.
@H2CO3 I can't see any good reason for anyone new to the platform to not use ARC, to be honest...but this discussion is probably best had somewhere else!
@lxt Good reasons not to begin with ARC: anyone new to the platform will 1. get lost with all the __bridge_transfer_nonretaining_autoreleasing_weak_strong_semi-strong casts, and 2. he will also not learn managing memory manually, which is an obligatory piece of knowledge, especially on embedded.
|
0

Scope. NSMutableArray *questions declares a variable local to the - didLoadFromCCB method. It doesn't set the instance variable (a variable with a smaller scope suppresses a variable with the same name but a wider scope). Simply write

self.questions = [[NSMutableArray alloc] initWithObject:[NSNumber numberWithInteger:-1]];

instead.

9 Comments

If you aren't using ARC, this will probably cause a memory leak, the property increases the retain count +1 as well as the alloc instruction. So you end with an object with a Retain Count of 2 and that you'll probably only release one in the dealloc method.
@PaulN I know. (You honestly thought I didn't know that?) But apart from me, nobody seems to be using MRC any more. Too bad.
thank you both =) indeed the self.questions worked, when i click it does log the array, but it crashes my app right after that, do i need some kind of dealoc after using it?
@RicardoCerqueira No, you rather need an hour to read an enhearten basic Objective-C and Apple's memory management guide.
No, I imagined that you will know this for your 54.2k reputation but added the comment for other people like you and me that are still using MRC and that may reach this answer and copy your code. ;)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.