Files
purchases-ios/Sources/FoundationExtensions/Optional+Extensions.swift
T
NachoSoto 2f1a4e0be8 PaywallViewController: new initializer with Offering identifier (#3587)
Depends on #3586.

This will be used by the hybrid SDKs to be able to load paywalls with an
offering identifier.
2024-01-22 09:00:36 -08:00

47 lines
1022 B
Swift

//
// Copyright RevenueCat Inc. All Rights Reserved.
//
// Licensed under the MIT License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://opensource.org/licenses/MIT
//
// Optional+Extensions.swift
//
// Created by Nacho Soto on 3/30/22.
import Foundation
/// Protocol definition to be able to use `Optional` as a type.
internal protocol OptionalType {
associatedtype Wrapped
init(optional: Wrapped?)
var asOptional: Wrapped? { get }
}
extension Optional: OptionalType {
init(optional: Wrapped?) { self = optional }
var asOptional: Wrapped? { return self }
}
extension OptionalType {
/// - Returns: unwrapped value if present.
/// - Throws: `error` if the value is not present.
func orThrow(_ error: @autoclosure () -> Error) throws -> Wrapped {
if let value = self.asOptional {
return value
} else {
throw error()
}
}
}