它给出错误“线程1:EXC_BREAKPOINT(代码= EXC_I386_BPT,子代码= 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 `
有人可以帮我弄清楚出了什么问题吗?
答案 0 :(得分:2)
因为你正在使用[super dealloc]
我想你的项目是非ARC的。在这种情况下,当您使用xImg
创建oImg
和-imageNamed
时,您不会保留它们(并且在-dealloc
中您会释放它们。也许,您应该更好地阅读iOS中的内存管理?)。所以在-touchBegan
中你正在访问被解除分配的对象,这会导致错误。改为使用
oImg = [[UIImage imageNamed:@"O copy.jpg"] retain];
您还可以通过编辑将您的项目转换为ARC - >重构 - >转换为ARC。
另外,我强烈建议使用按钮来捕捉触摸,尽量避免复制粘贴代码,并且,如NSElvis所述,使用&&而不是&