Reputation: 935
I'm trying to use if let
Swift statement to use an optional if it's not equal to nil
. But for some reason, when I use it, Xcode shows the object is `<>z
I have a function which returns MyObject?
and I want to check if it is nil, and if it's not I want to use it.
I'm trying to do it like this:
if let anObject = self.myFunc() {
anObject //Xcode shows that anObject is not MyObject but <<error type>>
}
(I'm using SwiftUI
if that matters)
Does anyone knows why?
Upvotes: 1
Views: 171
Reputation: 258413
The if let
is not allowed within body
as is, so if you just need to conditionally shown some view on result of function, then the solution will be
if self.myFunc() != nil {
// some view here
}
Upvotes: 1