简体   繁体   中英

how do I create fresh NSMutableArray?

I have an NSMutableArray which only lasts during the session. Currently I create it like this

NSMutableArray *temp = [[NSMutableArray alloc] initWithCapacity:10];
    [self setScoreArray:temp];
    [temp release];

Problem is when I go to check each index I'm getting an array outofbounds error

NSNumber *previousScore = [[self scoreArray] objectAtIndex:[self quizNum]];
    if ( previousScore != nil ) 
    {
        [self clearQuizBtns];
        NSInteger previousScoreValue = [previousScore integerValue];
        [self selectButtonAtTag:previousScoreValue];
    }else {
        [self clearQuizBtns];
    }

I've read in other posts that initWithCapacity doesn't actually create the array. So what can I populate the array with initially? Thanks in advance.

Two ways:

first : to initiate array with default values of NSNull class

NSMutableArray *temp = [[NSMutableArray alloc] initWithCapacity:10];
for (int i = 0 ; i < 10 ; i++)
{
    [temp insertObject:[NSNull null] atIndex:i];
}

[self setScoreArray:temp];
[temp release];

and then to check: if object is kind of NSNull class means it was a never set before

id previousScore = [[self scoreArray] objectAtIndex:[self quizNum]];
if (![previousScore isKindOfClass:[NSNull class]]) 
{
    [self clearQuizBtns];
    NSInteger previousScoreValue = [(NSNumber *)previousScore integerValue];
    [self selectButtonAtTag:previousScoreValue];
}else {
    [self clearQuizBtns];
}

second : store scores in NSMutableDictionary and use NSNumber 's as keys

// scoreDictionary property of NSMutableDictionary class must be declared in self

NSNumber *previousScore = [self.scoreDictionary objectForKey:[NSNumber numberWithInt:[self quizNum]]];
if (previousScore != nil) 
{
    [self clearQuizBtns];
    NSInteger previousScoreValue = [previousScore integerValue];
    [self selectButtonAtTag:previousScoreValue];
}else {
    [self clearQuizBtns];
}

NSArray does not support "holes". The capacity is just a hint to the initializer.

You could either fill the array with placeholder objects or, more typically, change your algorithm to either fully prepopulate the array or to lazy load it linearly.

Your problem seems to be that you're never actually setting any score in the score array.. are you? NSArrays have an actual count of items in them, and accessing an index beyond that count will blow up, as you've seen. If there will only ever be a fixed (small) number of scores, like 10, then you could set them all initially to something default like:

for (int i = 0; i < 10; i++) {
    [temp addObject:[NSNumber numberWithInt:0]];
}

PS -initWithCapacity does "create the array", it just doesn't create any objects in the array. The capacity is a hint only.

使用arrayWithObject:或arrayWithObjects:方法可以为数组提供预先填充的值。

One cool thing about NSMutableArrays is that you can just do an "init" and the array will handle adding and removing objects on the fly. Remember that you generally addObject: or removeObjectAtIndex: when dealing with mutable arrays.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM