当随机数显示两次时,我的对象崩溃iPhone应用程序! :(

时间:2011-04-21 21:17:58

标签: iphone xcode random nsstring nsnumber

我正在按照惯例制作一个不错的应用程序,然后就会发生这种情况!你在我的应用程序中旋转了财富之轮,当旋转动画结束时,应用程序应该随机化一个数字,并显示匹配为数字索引的卡片。

卡片存储在NSMutableArray中,第一次调用时效果很好。例如。当应用程序随机化数字“1”卡号1被调用并显示没有问题! 但是一旦随机发生器再次点击“1”,应用程序就会在下面指定的行上崩溃。

这是代码,请帮助我:)。

NSNumber *cardNumber;
    cardNumber = [NSNumber numberWithUnsignedInt:arc4random()%[appDelegate.cardArray count]];

    NSLog(@"%@", cardNumber);

    SpinCard *selectedCard = [appDelegate.cardArray objectAtIndex:[cardNumber doubleValue]];

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    [snurrKnapp setAlpha:100];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:cardView cache:YES];
    [UIView commitAnimations];
    [snurrKnapp setEnabled:YES];
    if(![cardTitle.text isEqualToString:selectedCard.cardTitle]) { //Crashes here
        [cardTitle setText:selectedCard.cardTitle]; //If I remove the if-case it crashes here
        [cardContent setText:selectedCard.cardContent];
    }
    [selectedCard release];

这是我得到的错误:

2011-04-21 23:10:54.296 Snurra Flaskan[947:707] 0
2011-04-21 23:10:58.794 Snurra Flaskan[947:707] 2
2011-04-21 23:11:02.691 Snurra Flaskan[947:707] 1
2011-04-21 23:11:08.977 Snurra Flaskan[947:707] 2
warning: Unable to read symbols for /Developer/Platforms/iPhoneOS.platform/DeviceSupport/4.3.1 (8G4)/Symbols/Developer/usr/lib/libXcodeDebuggerSupport.dylib (file not found).
(gdb) 

1 个答案:

答案 0 :(得分:5)

我在Obj-C中很新,但这就是我认为正在发生的事情。

您不应致电[selectedCard release];

因为objectAtIndex没有为selectedCard分配新的SpinCard。你不应该发布它。这解释了第二次使用SpinCard的行为:你不能使用它,它已经被释放。