我刚接触MacOS和Xcode,以前从来没有用过。我必须为我的类制作一个使用Xcode的简单游戏。我已经设法设置了一切(使用VirtualBox),看了一些教程,做了一个简单的石头,纸,剪刀游戏。构建是成功的,但是当模拟器启动我的应用程序时,我得到一个关于我的三个按钮(Paper_Button)的错误。即使在ViewController中注释按钮的代码,仍然会得到错误。我查了一下我的代码,但是什么也没有...我为这个愚蠢的问题感到抱歉,但我似乎没有找到任何解决办法。
这是一个错误:
异常NSException*“[
//
// ViewController.swift
// RPS
//
// Created by MaiorCristian on 4/21/21.
//
import UIKit
//@interface
//SettingsViewController:
//UITableViewController
//BEFORE: UIViewController
//AFTER: UITableViewController
class ViewController: UIViewController {
var player_points = 0
var cpu_points = 0
@IBOutlet weak var CPU_label: UILabel!
@IBOutlet weak var YOU_label: UILabel!
@IBOutlet weak var CPU_Image: UIImageView!
@IBOutlet weak var WINNER: UILabel!
var playersChoice = 0;
@IBAction func ROCK_Button(_ sender: Any) {
playersChoice = 1
let cpuNumberChoice = Int.random(in: 1...3)
setCPUimage(imageView: CPU_Image, imageNumber: cpuNumberChoice)
//rock 1, paper 2, scissors 3
//rock - rock
if cpuNumberChoice == 1 && playersChoice == 1 {
//tie
WINNER.text = "WINNER: TIE!"
}
//paper - rock
if cpuNumberChoice == 2 && playersChoice == 1 {
cpu_points = cpu_points + 1
WINNER.text = "WINNER: CPU!" }
//scissors - rock
if cpuNumberChoice == 3 && playersChoice == 1 {
player_points = player_points + 1
WINNER.text = "WINNER: YOU!" }
YOU_label.text = "YOU: \(player_points)"
CPU_label.text = "CPU: \(cpu_points)"
}
/*@IBAction func PAPER_Button(_ sender: Any) {
playersChoice = 2
let cpuNumberChoice = Int.random(in: 1...3)
setCPUimage(imageView: CPU_Image, imageNumber: cpuNumberChoice)
//rock 1, paper 2, scissors 3
//paper - paper
if cpuNumberChoice == 2 && playersChoice == 2 {
WINNER.text = "WINNER: TIE!" }
//scissors - papper
if cpuNumberChoice == 3 && playersChoice == 2 {
cpu_points = cpu_points + 1
WINNER.text = "WINNER: CPU!" }
//rock - papper
if cpuNumberChoice == 1 && playersChoice == 2 {
player_points = player_points + 1
WINNER.text = "WINNER: YOU!" }
YOU_label.text = "YOU: \(player_points)"
CPU_label.text = "CPU: \(cpu_points)"
}
*/
@IBAction func SCISSORS_Button(_ sender: Any) {
playersChoice = 3
let cpuNumberChoice = Int.random(in: 1...3)
setCPUimage(imageView: CPU_Image, imageNumber: cpuNumberChoice)
//rock 1, paper 2, scissors 3
//scissors - scissors
if cpuNumberChoice == 3 && playersChoice == 3 {
WINNER.text = "WINNER: TIE!" }
//rock - scissors
if cpuNumberChoice == 1 && playersChoice == 3 {
cpu_points = cpu_points + 1
WINNER.text = "WINNER: CPU!" }
//paper - scissors
if cpuNumberChoice == 2 && playersChoice == 3 {
player_points = player_points + 1
WINNER.text = "WINNER: YOU!" }
YOU_label.text = "YOU: \(player_points)"
CPU_label.text = "CPU: \(cpu_points)"
}
///
//
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
//@IBAction func
func setCPUimage(imageView:UIImageView, imageNumber: Int){
switch imageNumber {
case 1://rock
imageView.image = UIImage(named: "IMG_20210421_121204_edit_267680766477383")
case 3://scissors
imageView.image = UIImage(named: "IMG_20210421_121232_edit_267656474039366")
case 2://paper
imageView.image = UIImage(named: "IMG_20210421_121216_edit_267673424825822")
default:
print("error")
//imageView.image = UIImage(named: "rock")
}
}
}
libc++abi.dylib: terminating with uncaught exception of type NSException
***由于未捕获异常“NSunKnownKeyException”而终止应用程序,原因:“[
该错误表示由.xib或.storyboard文件引用的Paper_Button
在ViewController
类中不存在。
我猜从.xib或.storyboard文件中删除paper_button
将修复该错误。