在循环中添加到 NSMutableArray 时出现问题
我不确定这个循环到底有什么问题,但每当我运行它时,我都会收到 SIGABRT 信号。根据日志,问题是当我尝试将 NSNumber 添加到循环末尾附近的 NSMutable 数组时。显然我犯了一个基本错误,但我不确定问题是什么。
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, but 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 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, but 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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我确信这是一个拼写错误,但您没有分配指针,甚至不是
NSMutableArray
类型,而是分配NSArray
类型。检查行号2:应该是
I'm sure this is a typo, but you're not allocating a pointer and not even a type of
NSMutableArray
, but of typeNSArray
. Check line no. 2:It should be
您是否正在创建 NSArray 并将其分配给 NSMutableArray?也许您的意思如下:
Are you creating an NSArray and assigning it to an NSMutableArray? Perhaps you meant the following:
您正在释放 allValuesMArray: [ allValuesMArray release];
由于此时保留计数为 0,因此这将立即释放数组。
尝试使用 [ allValuesMArray autorelease ]。这将在将来释放该数组,使调用方法有机会使用未释放的数组或保留该数组以供以后使用。
您还可以在将产品添加到数组之前检查它是否不为零。
而且你有内存泄漏。
产品 = [[NSNumber 分配] 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.
这是经过清理、无泄漏的形式的代码:
如果此代码出现问题,我敢打赌
[ourDictionary objectForKey:toString]
返回的数组至少包含一个不存在的实例t 一个 NSNumber — 您可以通过在[scaleFactor doubleValue]
处抛出NSException
来识别它。Here's your code in a cleaned-up, non-leaking form:
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 anNSException
being thrown at[scaleFactor doubleValue]
.