提问者:小点点

Xcode NSException和


我刚接触MacOS和Xcode,以前从来没有用过。我必须为我的类制作一个使用Xcode的简单游戏。我已经设法设置了一切(使用VirtualBox),看了一些教程,做了一个简单的石头,纸,剪刀游戏。构建是成功的,但是当模拟器启动我的应用程序时,我得到一个关于我的三个按钮(Paper_Button)的错误。即使在ViewController中注释按钮的代码,仍然会得到错误。我查了一下我的代码,但是什么也没有...我为这个愚蠢的问题感到抱歉,但我似乎没有找到任何解决办法。

这是一个错误:

异常NSException*“[SetValue:ForUndefinedKey:]:对于键Paper_Button,此类不符合键值编码.”0x0000600001AB83F0名称__NSCFConstantString*“NSunKnownKeyException”0x00007FFF801E80A0原因__NSCFString*“[SetValue:ForUndefinedKey:]:对于键Paper_Button,此类不符合键值编码.”0x00006000025E8280 UserInfo__nsDictionarYI*2键/值对0x00006000001A10C0保留__nsDictionarYM*2键/值对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

***由于未捕获异常“NSunKnownKeyException”而终止应用程序,原因:“[SetValue:ForUndefinedKey:]:对于键Paper_Button,此类不符合键值编码。”以未捕获的NSException CoreSimulator 732.18.6类型的异常终止-设备:iPhone 11(BCA23623-0F8A-46DA-BE7F-F3B81149500E)-运行时:iOS 14.4(18D46)-设备类型:iPhone 11***由于未捕获的异常“NSunKnownKeyException”终止应用程序,原因:“[SetValue:ForUndefinedKey:]:该类不符合密钥Paper_Button的密钥值编码。”以NSException CoreSimulator 732.18.6类型的未捕获异常终止-设备:iPhone 11(BCA23623-0F8A-46DA-BE7F-F3B81149500E)-运行时:iOS 14.4(18D46)-设备类型:iPhone 11(lldb)


共1个答案

匿名用户

该错误表示由.xib或.storyboard文件引用的Paper_ButtonViewController类中不存在。

我猜从.xib或.storyboard文件中删除paper_button将修复该错误。