不明白为什么我在xCode中收到EXC_BAD_ACCESS

时间:2012-04-10 02:30:33

标签: objective-c xcode

我不明白为什么在我的.m文件中调用mineHit方法时出现EXC BAD ACCESS错误。我知道它表明按钮阵列已经发布,但我不明白为什么它会被释放。

#import "basicsViewController.h"

@implementation basicsViewController
@synthesize resetGame;
@synthesize scoreLabel;
@synthesize timeLabel;
@synthesize time;
@synthesize score;

-(void)newGame{
int index=0;
int yAxis=70;
for(int y=0;y<100;y=y+10){
    int xAxis=20;
    for( int x = 1; x < 11; x++) {
        buttonArray[index] = [[UIButton alloc]init];
        buttonArray[index] = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [buttonArray[index] setTag:index];
        [buttonArray[index] addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        buttonArray[index].frame = CGRectMake(xAxis, yAxis, 26.0, 26.0);
        NSLog(@"tag:%d xAxis:%d yAxis:%d",buttonArray[index].tag,(int)buttonArray[index].frame.origin.x,(int)buttonArray[index].frame.origin.y);
        [self.view addSubview:buttonArray[index]];
        xAxis=xAxis+28;
        index=x+y;
    }
    yAxis=yAxis+28;
}

//generate bombs

for (int bombs=0;bombs<10;bombs++){

    bombArray[bombs]= (arc4random()%99);
    //TODO compare against bombArray to make sure of no duplicates
    NSLog(@"BOMB AT %d",bombArray[bombs]);

}

}



- (IBAction)resetPress:(id)sender {
[self newGame];
}


- (void)buttonClicked:(UIButton*)button
{
BOOL hit;
NSLog(@"SELECTED BUTTON:%d",button.tag);
for (int b=0;b<10;b++){
    if (button.tag==bombArray[b]){
        //BOMB HIT
        hit=YES;
        b=10;
    }
    else {
        //no bomb
        hit=NO;
    }
}
if (hit==YES){
    //if hit
    NSLog(@"HIT AT %d",button.tag);
    [self mineHit];

}
else {
    //if not hit
    NSLog(@"%d is clean",button.tag);
    [self cleanHit:button];

}
}

-(void)mineHit{

for (int d=0;d<100;d++){
    NSLog(@"%i",buttonArray[d].tag);
    buttonArray[d].enabled=NO;
    [buttonArray[d] setTitle:@"*" forState:UIControlStateDisabled];
}

}
-(void)cleanHit:(UIButton*)button{
button.enabled=NO;
[button setTitle:@"!" forState:UIControlStateDisabled];

}

- (void)viewDidLoad
{
[super viewDidLoad];
[self newGame];

}

- (void)viewDidUnload
{
[self setResetGame:nil];
[self setScoreLabel:nil];
[self setTimeLabel:nil];
[super viewDidUnload];

}
@end

这是我的.h文件

#import <UIKit/UIKit.h>
NSInteger bombArray[];
UIButton *buttonArray[];
@interface basicsViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIButton *resetGame;
@property (weak, nonatomic) IBOutlet UILabel *scoreLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property int time;
@property int score;
-(void)newGame;
-(void)buttonClicked:(UIButton*)button;
-(void)mineHit;
-(void)cleanHit:(UIButton*)button;

@end

1 个答案:

答案 0 :(得分:1)

当我编译你的代码时,我得到四个警告。所有四个警告都是相同的并说:

  

假定暂定数组定义有一个元素

警告适用于接口(.h)文件中bombArraybuttonArray数组的定义。

如果我们给两个数组一个大小,那么-mineHit方法就可以了。

将.h文件的开头更改为:

#import <UIKit/UIKit.h>
NSInteger bombArray[10];
UIButton *buttonArray[100];
@interface basicsViewController : UIViewController

编译器出于某种原因生成警告,最好尝试让代码编译干净,没有任何警告或错误。

更新:虽然我们在这里没有理由你不能在界面内移动这些数组并将它们声明为实例变量。这样做意味着数组与视图控制器的单个实例相关联。您不太可能拥有此视图控制器的多个实例,但最好现在正确执行它而不是稍后被咬过。

#import <UIKit/UIKit.h>

@interface basicsViewController : UIViewController {
    NSInteger bombArray[10];
    UIButton *buttonArray[100];
}

有趣的是,将声明移到界面会将警告变为错误。