JSK
JSK

Reputation: 197

Value type is not bridged to Objective-C Error - Runtime error when using SKProductsRequest

I previously had been able to set up In-app purchases in a iOS app, however when coming back to the project and migrating to Swift 3 I am getting a run time error, there's no errors in the console but I am seeing

Value type is not bridged to Objective-C Error

This is for the following function for when I request the products

//Request Products
func requestProductInfo() {

    if SKPaymentQueue.canMakePayments() {

        let productIdentifiers = NSSet(array: productIDs)

        //ERROR
        let productRequest = SKProductsRequest(productIdentifiers: productIdentifiers as! Set<String>)

        productRequest.delegate = self
        productRequest.start()

    }
    else {
        print("Cannot perform In App Purchases.")
    }

}

Does anyone understand what's going on ? I understand that there may be some issues using a Swift Array and Set with a NSSet but I have tried to change things around but still was getting errors.

var productIDs: Array<String?> = []
var productsArray = [SKProduct]()

func transactionAction() {
    let payment = SKPayment(product: self.productsArray[self.selectedProductIndex] as SKProduct)
    SKPaymentQueue.default().add(payment)
    self.transactionInProgress = true

}

//Request Products
func requestProductInfo() {

    if SKPaymentQueue.canMakePayments() {

        let productIdentifiers = NSSet(array: productIDs)
        let productRequest = SKProductsRequest(productIdentifiers: productIdentifiers as! Set<String>)

        productRequest.delegate = self
        productRequest.start()

    }
    else {
        print("Cannot perform In App Purchases.")
    }

}

func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
    if response.products.count != 0 {
      //  print("\(response.products.map {p -> String in return p.localizedTitle})")

        productsArray = response.products
    }
    else {
        print("There are no products.")
    }

    if response.invalidProductIdentifiers.count != 0 {
        print("\(response.invalidProductIdentifiers.description)")
    }

    print("Number of products in productsArray \(productsArray.count) - Number of products in productIDs \(productIDs.count)")

    setPurchaseButton()
}

Upvotes: 1

Views: 655

Answers (1)

OOPer
OOPer

Reputation: 47876

Have your tried changing this line:

var productIDs: Array<String?> = []

to this?:

var productIDs: Array<String> = []

You may need to change some other parts according to this change, but with this you have no need to use risky as!:

        let productIdentifiers = Set(productIDs)
        let productRequest = SKProductsRequest(productIdentifiers: productIdentifiers)

If you need to pass Set<String> to a method, you'd better not use intermediate NSSet. The worst new feature of Swift 3, id-as-Any, would make something disastrous.

Upvotes: 1

Related Questions