我刚接触MacOS和Xcode,我以前从未用过它们。
我必须使用Xcode为我的类制作一个简单的游戏。我已经成功地设置了一切(使用VirtualBox),看了一些教程,并制作了一个简单的石头,布,剪刀游戏。
构建是成功的,但是当模拟器启动我的应用程序时,我得到一个关于我的三个按钮(Paper_Button)中的一个的错误。即使我在ViewController中注释按钮的代码,我仍然会得到错误。我在我的代码里查了一下,但是什么都没有...对于这个愚蠢的问题,我很抱歉,但我似乎没有找到任何解决方案。
这是错误:
Exception NSException * "[<RPS.ViewController 0x7fdf76008840> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key PAPER_Button." 0x0000600001ab83f0
name __NSCFConstantString * "NSUnknownKeyException" 0x00007fff801e80a0
reason __NSCFString * "[<RPS.ViewController 0x7fdf76008840> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key PAPER_Button." 0x00006000025e8280
userInfo __NSDictionaryI * 2 key/value pairs 0x00006000001a10c0
reserved __NSDictionaryM * 2 key/value pairs 0x00006000014c5fc0 //
// 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
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<RPS.ViewController 0x7fdf76008840> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key PAPER_Button.'
terminating with uncaught exception of type NSException
CoreSimulator 732.18.6 - Device: iPhone 11 (BCA23623-0F8A-46DA-BE7F-F3B81149500E) - Runtime: iOS 14.4 (18D46) - DeviceType: iPhone 11
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<RPS.ViewController 0x7fdf76008840> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key PAPER_Button.'
terminating with uncaught exception of type NSException
CoreSimulator 732.18.6 - Device: iPhone 11 (BCA23623-0F8A-46DA-BE7F-F3B81149500E) - Runtime: iOS 14.4 (18D46) - DeviceType: iPhone 11
(lldb) 发布于 2021-04-25 21:57:39
错误提示.xib或.storyboard文件引用的PAPER_Button在您的ViewController类中不存在。
我想从.xib或.storyboard文件中删除PAPER_Button可以修复这个错误。
https://stackoverflow.com/questions/67254083
复制相似问题