Ios Xcode NSException和NSUnknownKeyException
我不熟悉MacOS和Xcode,以前从未使用过它们 我必须使用Xcode为我的班级制作一个简单的游戏。我成功地设置了一切(使用VirtualBox),观看了一些教程并制作了一个简单的石头、布、剪刀游戏 建筑是成功的,但当模拟器启动我的应用程序时,我的三个按钮(Paper_按钮)中的一个出现错误。即使我在ViewController中注释按钮的代码,我仍然会得到错误。我查了我的代码,但什么都没有。。。我很抱歉这个愚蠢的问题,但我似乎没有找到任何解决办法 这就是错误:Ios Xcode NSException和NSUnknownKeyException,ios,swift,xcode,Ios,Swift,Xcode,我不熟悉MacOS和Xcode,以前从未使用过它们 我必须使用Xcode为我的班级制作一个简单的游戏。我成功地设置了一切(使用VirtualBox),观看了一些教程并制作了一个简单的石头、布、剪刀游戏 建筑是成功的,但当模拟器启动我的应用程序时,我的三个按钮(Paper_按钮)中的一个出现错误。即使我在ViewController中注释按钮的代码,我仍然会得到错误。我查了我的代码,但什么都没有。。。我很抱歉这个愚蠢的问题,但我似乎没有找到任何解决办法 这就是错误: Exception NSE
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
libc++abi.dylib:以NSException类型的未捕获异常终止
***由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[setValue:forUndefinedKey:]:此类不符合密钥纸张按钮的键值编码。”
以NSException类型的未捕获异常终止
CoreSimulator 732.18.6-设备:iPhone 11(BCA23623-0F8A-46DA-BE7F-F3B81149500E)-运行时:iOS 14.4(18D46)-设备类型:iPhone 11
***由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[setValue:forUndefinedKey:]:此类不符合密钥纸张按钮的键值编码。”
以NSException类型的未捕获异常终止
CoreSimulator 732.18.6-设备:iPhone 11(BCA23623-0F8A-46DA-BE7F-F3B81149500E)-运行时:iOS 14.4(18D46)-设备类型:iPhone 11
(lldb)
错误显示,.xib或.storyboard文件引用的纸张按钮在您的ViewController
类中不存在
我想从.xib或.storyboard文件中删除PAPER\u按钮将修复错误。转到Interface Builder,检查iAction是否正确连接。顺便说一下,根据命名约定,函数名应该是小写的。您有一个名为PAPER_Button
的变量,它是您在代码中创建的,在脚本中创建的,并在两者之间建立了连接。但您后来在代码中删除了它,但没有删除连接。这就是它失败的原因。对于其余部分,命名也很重要,因此强烈建议使用lowerCamelCase
。
//
// 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)