Question

why is offering an "error type"

  • 16 May 2024
  • 1 reply
  • 14 views

Badge +1
 static func availableOfferingWithIdentifier(_ id: String) -> Offering{

Purchases.shared.getOfferings { offering, error in

if let offerMatchingID = offering!.offering(identifier: id){
return offerMatchingID
}

}
}

Trying to display a Paywall for my offering with an identifier. produces error

 

 

why doesn't the closure “offering” realise it’s own type?


This post has been closed for comments

1 reply

Userlevel 6
Badge +8

Hey @t1esto!

It looks like your function has a return type of `Offering`, but calling getOfferings is an async method, so you won’t be able to return from the closure result. Instead, you’ll need to return the offering as part of a completion handler. Additionally, you’re force unwrapping the `offerings` result, which may result in crashes.

I’d recommend using a method like this instead:
 

static func availableOfferingWithIdentifier(_ id: String, completion: @escaping (Offering?) -> Void) {
Purchases.shared.getOfferings { offerings, error in
guard let offerings = offerings else {
completion(nil) // Handle the error or pass nil if offerings are unavailable
return
}

if let offerMatchingID = offerings.offering(identifier: id) {
completion(offerMatchingID) // Pass the result using the completion handler
} else {
completion(nil) // Pass nil if no matching offering is found
}
}
}