Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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:

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
            }

        }

    }

}
like image 828
Aytac Eren Avatar asked Nov 06 '17 17:11

Aytac Eren


1 Answers

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

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.

like image 193
TheAppMentor Avatar answered Sep 22 '22 06:09

TheAppMentor