Question

Having trouble initiating payment UI for monthly subscription

  • 16 April 2022
  • 1 reply
  • 25 views

Badge
I am trying to implement a monthly subscription as part of an in-app purchase.
 I am using a combination of xcode/swift and RevenueCat to implement this feature. In implementing the code that runs when the user clicks the purchase button i get the error saying "Fatal error: NSArray element failed to match the Swift Array Element type
Expected Package but found RCPackage". 

Where the code for the purchase button click is highlighted. Code is "guard let package = offerings.all.first?.value.availablePackages.first ".

I am not sure what to do to fix this issue.

 

 


1 reply

Userlevel 4
Badge +9

Sorry for the delay! Are you still seeing this issue? I’m not sure why Xcode is giving you that error. I just tried your code snippet and included a print, and the code ran and printed successfully:

What happens if you use [0] instead of .first? So instead of this:

guard let package = offerings.all.first?.value.availablePackages.first

Try this:

guard let package = offerings.all.first?.value.availablePackages[0]

I tried this on version 4.3.0 of the SDK using Xcode 13.3.1.

Here’s the code snippet I used so you can easily copy and paste to rule out any syntax errors:

Purchases.shared.offerings { offerings, error in
guard let offerings = offerings, error == nil else {
return
}
guard let package = offerings.all.first?.value.availablePackages.first else {
return
}
print(package.identifier)
}

 

 

Reply