开发者

out of scope - NSMutableArray error

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. 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.


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];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜