我尝试编写游戏代码(我使用本教程https://www.youtube.com/watch?v=LkYpoRj-7hA我是 Swift 中的新手)。到目前为止,这就是我的代码:
import UIKit
class ViewController: UIViewController {
//Image View
@IBOutlet var ticTac_A01: UIImageView = nil
@IBOutlet var ticTac_A02: UIImageView = nil
@IBOutlet var ticTac_A03: UIImageView = nil
@IBOutlet var ticTac_B01: UIImageView = nil
@IBOutlet var ticTac_B02: UIImageView = nil
@IBOutlet var ticTac_B03: UIImageView = nil
@IBOutlet var ticTac_C01: UIImageView = nil
@IBOutlet var ticTac_C02: UIImageView = nil
@IBOutlet var ticTac_C03: UIImageView = nil
//Buttons
@IBOutlet var ticTacBtn_A01: UIButton = nil
@IBOutlet var ticTacBtn_A02: UIButton = nil
@IBOutlet var ticTacBtn_A03: UIButton = nil
@IBOutlet var ticTacBtn_B01: UIButton = nil
@IBOutlet var ticTacBtn_B02: UIButton = nil
@IBOutlet var ticTacBtn_B03: UIButton = nil
@IBOutlet var ticTacBtn_C01: UIButton = nil
@IBOutlet var ticTacBtn_C02: UIButton = nil
@IBOutlet var ticTacBtn_C03: UIButton = nil
@IBOutlet var resetBtn: UIButton = nil
@IBOutlet var userMessage: UILabel = nil
var plays = Dictionary<Int, Int>()
var done = false;
var aiDeciding = false;
@IBAction func UIButtonClicked(sender : UIButton){
userMessage.hidden = true;
if !plays[sender.tag] && !aiDeciding && !done {
setImageForSpot(sender.tag, player:1)
}
checkForWin()
aiTurn()
}
func setImageForSpot(spot:Int, player:Int){
var playerMark = player == 1 ? "x" : "o"
plays[spot] = player
switch spot {
case 1:
ticTac_A01.image = UIImage(named: playerMark)
case 2:
ticTac_A02.image = UIImage(named: playerMark)
case 3:
ticTac_A03.image = UIImage(named: playerMark)
case 4:
ticTac_B01.image = UIImage(named: playerMark)
case 5:
ticTac_B02.image = UIImage(named: playerMark)
case 6:
ticTac_B03.image = UIImage(named: playerMark)
case 7:
ticTac_C01.image = UIImage(named: playerMark)
case 8:
ticTac_C02.image = UIImage(named: playerMark)
case 9:
ticTac_C03.image = UIImage(named: playerMark)
default:ticTac_B02.image = UIImage(named: playerMark)
}
}
func checkForWin(){}
func aiTurn(){}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
首先 ii 使用以下输出获取“线程 1:信号 SIGABRT”:
2014-07-21 10:26:21.709 Swift-Tac-Toe[3264:107207] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<_TtC13Swift_Tac_Toe14ViewController 0x7f8160ca0f00> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key ticTac01.'
*** First throw call stack:
(
0 CoreFoundation 0x00000001010a3055 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000102b5da1c objc_exception_throw + 45
2 CoreFoundation 0x00000001010a2c99 -[NSException raise] + 9
3 Foundation 0x00000001014b67a3 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 259
4 CoreFoundation 0x0000000100fed950 -[NSArray makeObjectsPerformSelector:] + 224
5 UIKit 0x0000000101bdb58d -[UINib instantiateWithOwner:options:] + 1506
6 UIKit 0x0000000101a42898 -[UIViewController _loadViewFromNibNamed:bundle:] + 242
7 UIKit 0x0000000101a42f49 -[UIViewController loadView] + 109
8 UIKit 0x0000000101a431ba -[UIViewController loadViewIfRequired] + 75
9 UIKit 0x0000000101a4364f -[UIViewController view] + 27
10 UIKit 0x0000000101964d79 -[UIWindow addRootViewControllerViewIfPossible] + 58
11 UIKit 0x0000000101965112 -[UIWindow _setHidden:forced:] + 276
12 UIKit 0x0000000101971e70 -[UIWindow makeKeyAndVisible] + 42
13 UIKit 0x000000010191cc31 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 2673
14 UIKit 0x000000010191f5cc -[UIApplication _runWithMainScene:transitionContext:completion:] + 1222
15 UIKit 0x000000010191e5ac -[UIApplication workspaceDidEndTransaction:] + 19
16 FrontBoardServices 0x0000000104357263 __31-[FBSSerialQueue performAsync:]_block_invoke + 16
17 CoreFoundation 0x0000000100fd909c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
18 CoreFoundation 0x0000000100fce805 __CFRunLoopDoBlocks + 341
19 CoreFoundation 0x0000000100fce5c5 __CFRunLoopRun + 2389
20 CoreFoundation 0x0000000100fcda06 CFRunLoopRunSpecific + 470
21 UIKit 0x000000010191dfc2 -[UIApplication _run] + 413
22 UIKit 0x0000000101920cf8 UIApplicationMain + 1282
23 Swift-Tac-Toe 0x0000000100c224fd top_level_code + 77
24 Swift-Tac-Toe 0x0000000100c2253a main + 42
25 libdyld.dylib 0x00000001030ce145 start + 1
26 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
在 Debug > Continue 我得到Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)。
就是这样。
任何人都可以在这里帮助我吗?