0
data = [[NSMutableArray arrayWithCapacity:numISF]init];
count = 0;
while (count <= numISF)
{   
    [data addObject:[[rouge_col_data alloc]init]];
    count++;
}

When I step through the while loop, each object in the data array is 'out of scope'

rouge col data 's implementation looks like this..

@implementation rouge_col_data
@synthesize pos;
@synthesize state;
-(id) init {
    self = [super init];    
    return self;
}
@end

Most tutorials I could find only use NSStrings for objects in these kinds of arrays.

-Thanks Alex E

EDIT

data = [[[NSMutableArray alloc] initWithCapacity:numISF]retain];
//data = [[NSMutableArray arrayWithCapacity:numISF] retain];
count = 0;
while (count < numISF)
{

    [data addObject:[[[rouge_col_data alloc]init]autorelease]];

    count++;

}

still the same error, even when switching the 'data = '.

1
  • Is there a better way to fix this? Commented Nov 12, 2009 at 18:39

2 Answers 2

3
  1. You don't need to call init on the result of your arrayWithCapacity: call. arrayWithCapacity: already returns you an initialized (but autoreleased) object. Alternatively you could call [[NSMutableArray alloc] initWithCapacity:].
  2. Your loop has an off by one error; you're starting at zero, so you'll add an extra object. Adding this extra object will succeed - it just doesn't seem like what you're trying to do.
  3. You probably want to autorelease the objects you're adding to the array. The array will retain them on its own. If you do have some need to retain the objects themselves, that's fine, but it's pretty common to let the array do the retention for you.
  4. You should retain the array itself, otherwise it will vanish at the end of the event loop.
Sign up to request clarification or add additional context in comments.

Comments

2

The only error I can spot in your code is your NSArray initialization.

Where you do:

data = [[NSMutableArray arrayWithCapacity:numISF] init];

you should be doing:

data = [NSMutableArray arrayWithCapacity:numISF];

This is because arrayWithCapacity is a factory method, and will return you an autoreleased instance. If you want to keep using the object after this method, you'll need to retain it, and your could will look like:

data = [[NSMutableArray arrayWithCapacity:numISF] retain];

1 Comment

Whether you need to retain the array depends on whether you need it after the method finishes.

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.