You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
session-ios/SessionUtilitiesKit/Utilities/Optional+Utilities.swift

24 lines
597 B
Swift

// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
import Foundation
extension Optional {
public func map<U>(_ transform: (Wrapped) throws -> U?) rethrows -> U? {
switch self {
case .some(let value): return try transform(value)
default: return nil
}
}
public func asType<R>(_ type: R.Type) -> R? {
switch self {
case .some(let value): return (value as? R)
default: return nil
}
}
public func defaulting(to value: Wrapped) -> Wrapped {
return (self ?? value)
}
}