Thursday, 15 July 2010

xcode - EXC_BAD_INSTRUCTION error in tic tac toe -



xcode - EXC_BAD_INSTRUCTION error in tic tac toe -

after press 'play again' button in tic tac toe game suppose reset every button.

when :

for var = 1; < 9; i++

it not crash, hides except upper left button.

when

for var = 0; < 9; i++

it crashes , indicates exc_bad_instruction

apparently breakpoint here :

if (gamestate[sender.tag]==0 && winner == 0) {

this playagainpressed action

@ibaction func playagainpressed(sender : anyobject) { gonumber = 1 winner = 0 gamestate = [0, 0, 0, 0, 0, 0, 0, 0, 0] label.center = cgpointmake(label.center.x - 400, label.center.y) playagain.alpha = 0 var button : uibutton var = 0; < 9; i++ { button = view.viewwithtag(i) uibutton button.setimage(nil, forstate: .normal) } }

0x108b7d350: ud2

if (gamestate[sender.tag]==0 && winner == 0) {

any thoughts?

i changed tags 100, 101 etc

@ibaction func buttonpressed(sender: anyobject) { if (gamestate[sender.tag]==0){ var image = uiimage() if (gonumber%2==0){ image = uiimage(named: "o")! gamestate[sender.tag]==2 }else{ image = uiimage(named: "x")! gamestate[sender.tag]==1 } gonumber++ sender.setimage(image, forstate: .normal) } }

it giving error

fatal error: array index out of range (lldb)

and calling exc_bad_instruction again. have no thought wrong it.

if (gamestate[sender.tag]==0)

this indicates there's wrong. marks part above greenish , calls exc_bad_instruction..

best guess have multiple views tag value 0 since default. getting view not button. or there may not exist.

viewwithtag returns optional , not testing nil. don't ignore testing optionals , don't add together explicit unwrapping, toss optional protections.

when using tags avoid 0. improve yet utilize larger tags like: 100, 101, 102 ...

xcode swift

No comments:

Post a Comment