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

6

以下是我收到的具体错误信息:

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个回答

8

在你的Storyboard中右键点击按钮,你应该能看到一个名叫Label2的outlet。删除它,然后一切都会好起来。

这通常发生在你有一个与代码连接的outlet,但是你在代码中删除/重命名了outlet并忘记从storyboard中删除它。


谢谢,这完美地解决了问题!它对我帮助很大!;) - Aytac Eren

0

查看您的 StoreViewController 的故事板 / xib。基本上,您有一个 Label2,它应该连接到 ViewController,但在代码中不存在。 @IBOutlet Label2 可能已被删除、重命名或注释掉。

要修复它,您可以通过右键单击曾经是 Label2 的元素并从那里删除引用来从故事板或 Xib 中删除该损坏的链接。


谢谢你的帮助!它对我有用!;) - Aytac Eren
欢迎您,@AytacEren!这种情况经常发生...哈哈。:D - Khanal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接