由于未捕获的异常终止应用程序' NSUnknownKeyException',原因:此类不符合键值Label2的键值编码。'

时间:2017-11-06 17:28:40

标签: ios iphone swift xcode debugging

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

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.'

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

这是我在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
            }

        }

    }

}

2 个答案:

答案 0 :(得分:3)

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

当您的插座连接到您的代码时,通常会发生这种情况,但是您要么删除/重命名代码中的插座而忘记将其从故事板中删除。

答案 1 :(得分:0)

查看StoreViewController的storyboard / xib。基本上有一个断开的链接,你有一个git branch -a据说连接到ViewController但它不在代码中。 Label2被删除,重命名或被注释掉。

你可以做些什么来修复它是通过右键单击曾经是Label2的元素并从那里删除引用来从Storyboard或Xib中删除那个断开的链接。