0

它给出错误“线程 1:EXC_BREAKPOINT (code=EXC_I386_BPT,subcode=0x0) 我的代码:

@synthesize s1;
@synthesize s2;
@synthesize s3;
@synthesize s4;
@synthesize s5;
@synthesize s6;
@synthesize s7;
@synthesize s8;
@synthesize s9;
@synthesize oImg,xImg,theImg,whoseTurn,board;
@synthesize resetButton, myAlertView;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initalization
    }
    return self;
}

- (void)viewDidLoad {
    oImg = [UIImage imageNamed:@"O copy.jpg"];
    xImg = [UIImage imageNamed:@"X copy.jpg"];

    playerToken = 1;

    whoseTurn.text = @"X can go";

    numberOfPlays = 0;


    [super viewDidLoad];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    switch(playerToken){
        case 1:theImg = xImg;
            break;
        case 2:
            theImg = oImg;
            break;
    }

    UITouch *touch = [[event allTouches]anyObject];
    cellWasUsed = NO;

    if (CGRectContainsPoint([s1 frame], [touch locationInView:self.view])&(s1.image == NULL)) {
        cellWasUsed = YES;
        s1.image = theImg;
    }
    if (CGRectContainsPoint([s2 frame], [touch locationInView:self.view])&(s2.image == NULL)) {
        cellWasUsed = YES;
        s2.image = theImg;
    }
    if (CGRectContainsPoint([s3 frame], [touch locationInView:self.view])&(s3.image == NULL)) {
        cellWasUsed = YES;
        s3.image = theImg;
    }
    if (CGRectContainsPoint([s4 frame], [touch locationInView:self.view])&(s4.image == NULL)) {
        cellWasUsed = YES;
        s4.image = theImg;
    }
    if (CGRectContainsPoint([s5 frame], [touch locationInView:self.view])&(s5.image == NULL)) {
        cellWasUsed = YES;
        s5.image = theImg;
    }
    if (CGRectContainsPoint([s6 frame], [touch locationInView:self.view])&(s6.image == NULL)) {
        cellWasUsed = YES;
        s6.image = theImg;
    }
    if (CGRectContainsPoint([s7 frame], [touch locationInView:self.view])&(s7.image == NULL)) {
        cellWasUsed = YES;
        s7.image = theImg;
    }
    if (CGRectContainsPoint([s8 frame], [touch locationInView:self.view])&(s8.image == NULL)) {
        cellWasUsed = YES;
        s8.image = theImg;
    }
    if (CGRectContainsPoint([s9 frame], [touch locationInView:self.view])&(s9.image == NULL)) {
        cellWasUsed = YES;
        s9.image = theImg;
    }
    [self processLogic];
    if (cellWasUsed) {
        [self updatePlayerInfo];
    }
}

-(void)processLogic{

    if ([self checkForWin]){
        if(playerToken ==1){
            myAlertView = [[UIAlertView alloc] initWithTitle:@"winner" message:@"X won" delegate:self
                                           cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
            [myAlertView show];
            [self resetBoard];
        }
        else if(playerToken ==2){
            myAlertView = [[UIAlertView alloc] initWithTitle:@"winner" message:@"O won" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
            [myAlertView show];
            [self resetBoard];
        }
        if(numberOfPlays ==9){
            myAlertView = [[ UIAlertView alloc]initWithTitle:@"No Winner" message:@"Tie" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
            [myAlertView show];

            [self resetBoard];
        }



    }

}

-(BOOL)checkForWin{

    // HORIZONTAL WINS
    if((s1.image == s2.image) & (s2.image == s3.image) & (s1.image != NULL))
    {
        return YES;
    }
    if((s4.image == s5.image) & (s5.image == s6.image) & (s4.image != NULL))
    {
        return YES;
    }
    if((s7.image == s8.image) & (s8.image == s9.image) & (s7.image != NULL))
    {
        return YES;
    }
    // VERTICAL WINS
    if((s1.image == s4.image) & (s4.image == s7.image) & (s1.image != NULL))
    {
        return YES;
    }
    if((s2.image == s5.image) & (s5.image == s8.image) & (s2.image != NULL))
    {
        return YES;
    }
    if((s3.image == s6.image) & (s6.image == s9.image) & (s3.image != NULL))
    {
        return YES;
    }
    // DIAGONAL WINS
    if((s1.image == s5.image) & (s5.image == s9.image) & (s1.image != NULL))
    {
        return YES;
    }
    if((s3.image == s5.image) & (s5.image == s7.image) & (s3.image != NULL))
    {
        return YES;
    }
    //right now return 1 becuase we havn't implemented this yet
    return NO;
}

-(void)displayWinner{
    if([self checkForWin]==YES){
        if(playerToken ==1){
            whoseTurn.text =@"X is the winner!";

        }
        else{whoseTurn.text = @"O is the winner!";

        }
    }

}

-(IBAction)buttonReset{

    [self resetBoard];

}

-(void)resetBoard{

    s1.image =NULL;
    s2.image =NULL;
    s3.image =NULL;
    s4.image =NULL;
    s5.image =NULL;
    s6.image =NULL;
    s7.image =NULL;
    s8.image =NULL;
    s9.image =NULL;

    playerToken =1;
    whoseTurn.text = @"X can go";

    numberOfPlays = 0;






}

-(void)updatePlayerInfo{
    numberOfPlays++;
    if(numberOfPlays ==9){

        [self resetBoard];
    }
    if (playerToken ==1){

        playerToken =2;
        whoseTurn.text = @"O can go";

    } else {
        playerToken = 1;
        whoseTurn.text = @"X can go";
    }
}

- (void)dealloc {
    [s1 release];
    [s2 release];
    [s3 release];
    [s4 release];
    [s5 release];
    [s6 release];
    [s7 release];
    [s8 release];
    [s9 release];
    [theImg release];
    [resetButton release];
    [board release];
    [oImg release];
    [xImg release];
    [whoseTurn release];
    [myAlertView release];



    [super dealloc];


}


@end `

有人可以帮我找出问题所在吗?

4

1 回答 1

2

因为你正在使用[super dealloc]我猜你的项目是非 ARC。在这种情况下,当您创建xImgoImg使用-imageNamed时不要保留它们(并且在-dealloc您释放它们时。也许,您应该更正确地阅读有关 iOS 中的内存管理的信息?)。因此,-touchBegan您正在访问已释放的对象,这会导致错误。改为使用

oImg = [[UIImage imageNamed:@"O copy.jpg"] retain];

您还可以通过 Edit -> Refactor -> Convert to ARC 将项目转换为 ARC。

此外,我强烈建议使用按钮来捕捉触摸,尽量避免复制粘贴代码,并且正如 NSElvis 提到的,使用 && 而不是 &

于 2013-08-31T22:36:36.300 回答