xcode 由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:此类与键 Label2 的键值编码不兼容。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47142529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 10:24:46  来源:igfitidea点击:

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: this class is not key value coding-compliant for the key Label2.'

iosiphoneswiftxcodedebugging

提问by Aytac Eren

Here's the exact error message I got:

这是我收到的确切错误消息:

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[< Deetox.StoreViewController 0x102640610 > setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key Label2.'

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[< Deetox.StoreViewController 0x102640610 > setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key Label2.'

I created an info button which is suppose to take the user to the Store page of my app but when I tap on the info button it crashes. Do you guys know why? And how can I fix it? Thanks a lot! ;)

我创建了一个信息按钮,它应该将用户带到我的应用程序的商店页面,但是当我点击信息按钮时它崩溃了。大家知道为什么吗?我该如何解决?非常感谢!;)

Here's my code on StoreViewController:

这是我在 StoreViewController 上的代码:

import UIKit

import StoreKit

class StoreViewController: UIViewController, SKPaymentTransactionObserver, SKProductsRequestDelegate {

    func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
    }

    @IBOutlet var Label1: UILabel!
    @IBOutlet var Button1: UIButton!
    @IBOutlet var Button2: UIButton!
    @IBOutlet var BuyButton: UIButton!
    @IBOutlet var ProductTitle: UILabel!
    @IBOutlet var ProductDescription: UITextView!

    var Product: SKProduct?
    var ProductID = "co.AytacEren.Deetox.RemoveAds"

    override func viewDidLoad() {
        super.viewDidLoad()

        Label1.layer.cornerRadius = 5.0
        Button1.layer.cornerRadius = 5.0
        Button2.layer.cornerRadius = 5.0
        BuyButton.layer.cornerRadius = 5.0

        BuyButton.isEnabled = false
        SKPaymentQueue.default().add(self)
        getPurchaseInfo()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func DismissView(_ sender: Any) {

        self.dismiss(animated: true, completion: nil)

    }

    @IBAction func Purchase(_ sender: Any) {

        let Payment = SKPayment(product: Product!)
        SKPaymentQueue.default().add(Payment)

    }

    @IBAction func Restore(_ sender: Any) {

        SKPaymentQueue.default().restoreCompletedTransactions()

    }


    func getPurchaseInfo() {

        if SKPaymentQueue.canMakePayments() {

            let request = SKProductsRequest(productIdentifiers: NSSet(objects: self.ProductID) as! Set<String>)

            request.delegate = self
            request.start()

        }

        else {

            ProductTitle.text = "Warning"
            ProductDescription.text = "Please enable In-App Purchase in your settings"

        }

    }

    func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {

        var products = response.products

        if (products.count == 0) {

            ProductTitle.text = "Warning"
            ProductDescription.text = "Product not found"

        }

        else {

            Product = products [0]
            ProductTitle.text = Product!.localizedTitle
            ProductDescription.text = Product?.localizedDescription
            BuyButton.isEnabled = true

        }

        let Invalids = response.invalidProductIdentifiers

        for Product in Invalids {

            print("Product not found: \(Product)")

        }

    }

    func paymentQueue(_ queue: SKPaymentQueue, removedTransactions transactions: [SKPaymentTransaction]) {

        for transaction in transactions {

            switch transaction.transactionState {

            case SKPaymentTransactionState.purchased:

                SKPaymentQueue.default().finishTransaction(transaction)
                ProductTitle.text = "Thank you"
                ProductDescription.text = "You have purchased the product"
                BuyButton.isEnabled = false

                let save = UserDefaults.standard
                save.set(true, forKey: "Purchase")
                save.synchronize()

            case SKPaymentTransactionState.restored:

                SKPaymentQueue.default().finishTransaction(transaction)
                ProductTitle.text = "Thank you"
                ProductDescription.text = "You have purchased the product"
                BuyButton.isEnabled = false

                let save = UserDefaults.standard
                save.set(true, forKey: "Purchase")
                save.synchronize()

            case SKPaymentTransactionState.failed:

                SKPaymentQueue.default().finishTransaction(transaction)
                ProductTitle.text = "Warning"
                ProductDescription.text = "You have not purchased the product"

            default:
                break
            }

        }

    }

}

回答by TheAppMentor

In your storyboard right click on the button. You should seen an outlet called Label2. Delete it, and you should be fine.

在故事板中右键单击按钮。您应该看到一个名为 的插座Label2。删除它,你应该没问题。

This usually happens when you have an outlet that is connected to your code, but then you either delete/rename the outlet in your code and forget to delete it from the storyboard.

当您有一个连接到代码的插座时,通常会发生这种情况,但随后您在代码中删除/重命名插座并忘记从故事板中删除它。

回答by Khanal

Take a look at your storyboard / xib for StoreViewController. Basically there is a broken link where you have a Label2supposedly connected to the ViewController but it is not there in code. @IBOutlet Label2either got deleted, renamed or is commented out.

查看 StoreViewController 的故事板/xib。基本上有一个断开的链接,您Label2应该连接到 ViewController 但它不在代码中。@IBOutlet Label2要么被删除,重命名,要么被注释掉。

What you can do to fix it is remove that broken link from the Storyboard or Xib by right-clicking the the element that was once Label2 and removing the reference from there.

您可以通过右键单击曾经是 Label2 的元素并从那里删除引用来修复它,从而从 Storyboard 或 Xib 中删除该断开的链接。