【问题标题】:Thread 1: EXC_BREAKPOINT (code=EXC_I386_BPT, subcode=0x0)线程 1:EXC_BREAKPOINT(代码=EXC_I386_BPT,子代码=0x0)
【发布时间】:2013-08-31 22:00:43
【问题描述】:

它给出错误“线程 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 `

谁能帮我找出问题所在?

【问题讨论】:

  • 而不是单个 &: if((s3.image == s5.image) & (s5.image == s7.image) & (s3.image != NULL)) 使用双 &&: if((s3.image == s5.image) && (s5.image == s7.image) && (s3.image != NULL))
  • 你不能发布一堆代码然后说“怎么了?”。至少提供有关完整错误消息的更多详细信息,并指出代码中崩溃的位置(基于堆栈跟踪)。

标签: objective-c ios6.1


【解决方案1】:

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

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

您还可以通过编辑 -> 重构 -> 转换为 ARC 将项目转换为 ARC。

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

【讨论】:

  • @medvedNick 非常感谢!!
猜你喜欢
  • 2012-11-11
  • 1970-01-01
  • 2015-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多