[Solved] Issue about extending Optional with Generic Type in Swift


extension Optional {
    func safeUnwrap(_ defaultValue: Wrapped) -> Wrapped {
        switch self {
            case let value?: return value
            case nil: return defaultValue
        }
    }
}

Or even

extension Optional {
  func safeUnwrap(_ defaultValue: Wrapped) -> Wrapped {
    self ?? defaultValue
  }
}

But as was pointed out, this is more wordy and less idiomatic than just using the ?? operator.

solved Issue about extending Optional with Generic Type in Swift