Skip to main content
Question

why is offering an "error type"

  • 16 May 2024
  • 1 reply
  • 22 views

Forum|alt.badge.img+3
 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

cody
RevenueCat Staff
Forum|alt.badge.img+8
  • RevenueCat Staff
  • 492 replies
  • May 20, 2024

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
        }
    }
}

 


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings