Optional<T>
ma metodę map
.Jak mogę wywołać .map() na ImplicitlyUnwrappedOptional?
/// If `self == nil`, returns `nil`. Otherwise, returns `f(self!)`.
func map<U>(f: (T) -> U) -> U?
Gdy chcemy konwertować Int?
do UInt64?
, możemy:
let iVal:Int? = 42
let i64Val = iVal.map { UInt64($0) }
Zamiast:
var i64Val:UInt64?
if let iVal = iVal {
i64Val = UInt64(iVal)
}
Tutaj, ImplicitlyUnwrappedOptional<T>
ma tę samą metodę:
/// If `self == nil`, returns `nil`. Otherwise, returns `f(self!)`.
func map<U>(f: (T) -> U) -> U!
... Więc próbowałem i nie udało :(
let iVal:Int! = 42
let i64Val = iVal.map { UInt64($0) }
// ^ ~~~ [!] error: 'Int' does not have a member named 'map'
Oto pytanie: Jak można wywołać tej metody?
myślę posiadające funkcję map' 'na' 'ImplicitlyUnwrappedOptional jest bezcelowe. Kompilator już konwertuje go na wartość. –
mustafa