开发者

Problem Adding to NSMutableArray in Loop

I'm not sure what the problem is with this loop exactly, but I keep getting SIGABRT whenever I run it. According to the log the problem is when I try to add the NSNumber to the NSMutable array near the end of the loop. Obviously I've made an elementary error but I'm not sure what the problem is.

NSArray *toArray = [ourDictionary objectForKey:toString];
NSMutableArray *allValuesMArray = [[NSMutableArray alloc] init];
while (done == NO)
{
    if (i == 10)
        done = YES;
           /* 
            *The job here is to multiply these three numbers together and store the 
            *product in the mutable array. It tells me NSObject did not recognize selector
            *and then crashes.
            *original and multiplyFrom are always the same value, and multiplyTo is updated
            *from an array I made above from a p-list.
            *I'm hoping I didn't make a ton of rookie mistakes here, b开发者_Python百科ut I'm new to dealing with
            *NSMutableArray and such.
            */

    NSNumber *original = [NSNumber numberWithDouble:convertThis];
    NSNumber *multiplyFrom = [NSNumber numberWithDouble:multiply];
    NSNumber *multiplyTo = [NSNumber numberWithDouble:[[toArray objectAtIndex:i] doubleValue]];
    NSNumber *product = [[NSNumber alloc] init];

    product = [NSNumber numberWithDouble:([original doubleValue] * 
                                          [multiplyFrom doubleValue] *
                                          [multiplyTo doubleValue])];

    [allValuesMArray addObject:product];
            //This line ^^^ causes crash
    i++;
}
NSArray *returnThisArray = allValuesMArray;
[allValuesMArray autorelease];
return returnThisArray;


I'm sure this is a typo, but you're not allocating a pointer and not even a type of NSMutableArray, but of type NSArray. Check line no. 2:

NSMutableArray allValuesMArray = [[NSArray alloc] init];

It should be

NSMutableArray *allValuesMArray = [[NSMutableArray alloc] init];


Are you creating an NSArray and assigning it to an NSMutableArray? Perhaps you meant the following:

NSMutableArray* allValuesMArray = [[NSMutableArray alloc] init];


You are releasing the allValuesMArray: [ allValuesMArray release];

Since the retain count is 0 at this point, this will free the array immediately.

Try using [ allValuesMArray autorelease ]. This will release the array in the future, giving the opportunity for the calling method to use the unreleased array or retain the array for later use.

Also you could check if product is not nil before you add it to the array.

And you've got a memory leak.

product = [[NSNumber alloc] init];

then you assign it a different object later on.


Here's your code in a cleaned-up, non-leaking form:

NSMutableArray *convertedValues = [NSMutableArray array];
// moved outside of the loop and descriptively named:
double normalizedValue = convertThis * multiply;
// make use of NSFastEnumeration -- better readability _and_ reliability
for ( NSNumber *scaleFactor in [ourDictionary objectForKey:toString] )
{
    // just in case you want to add a breakpoint to see if everything works as expected:
    double convertedValue = normalizedValue * [scaleFactor doubleValue];
    [convertedValues addObject:[NSNumber numberWithDouble:convertedValue]];
}
return convertedValues;

If something goes wrong with this code, I bet that the array returned by [ourDictionary objectForKey:toString] contains at least one instance that isn't an NSNumber — which you will recognize by an NSException being thrown at [scaleFactor doubleValue].

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜