在循环中添加到 NSMutableArray 时出现问题

发布于 2024-10-13 07:35:39 字数 1528 浏览 9 评论 0原文

我不确定这个循环到底有什么问题,但每当我运行它时,我都会收到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

调妓 2024-10-20 07:35:39

我确信这是一个拼写错误,但您没有分配指针,甚至不是 NSMutableArray 类型,而是分配 NSArray 类型。检查行号2:

NSMutableArray allValuesMArray = [[NSArray alloc] init];

应该是

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

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];
大姐,你呐 2024-10-20 07:35:39

您是否正在创建 NSArray 并将其分配给 NSMutableArray?也许您的意思如下:

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];
请远离我 2024-10-20 07:35:39

您正在释放 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.

春花秋月 2024-10-20 07:35:39

这是经过清理、无泄漏的形式的代码:

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;

如果此代码出现问题,我敢打赌 [ourDictionary objectForKey:toString] 返回的数组至少包含一个不存在的实例t 一个 NSNumber — 您可以通过在 [scaleFactor doubleValue] 处抛出 NSException 来识别它。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文