I've been trying to find a way to check if SF Symbols and Images exist in swift.
In all of the answers I've come across, the nil coalescing operator (??
) is used, which requires a default
image that always exists. This doesn't work in my case.
I would like to test if an image (or a symbol) exists and then return it if it exists, otherwise return nil
.
How can this be achieved?
IMPORTANT NOTE
I am trying to achieve this using SwiftUI
's Image
for a multiplatform application. I am trying to avoid using UIImage
or NSImage
to do my checks.
UPDATE 1
Note 1: When requesting an SF Symbol that does not exist (for example, there was a typo in the systemName: [name]
parameter), the image appears as an empty area. (Tested with Label
)
The following solutions have been tested and are ineffective:
(Note: in all of the attemps, there are is a variable name: String
)
Solution 1
let image = Image(systemName: name)
if image {
return image
} else {
return nil
}
Throws the error:
Cannot convert value of type 'Image' to expected condition type 'Bool'
It throws the exact same error when: let image = Image(name)
Solution 2
if let image = Image(systemName: name) {
return image
}
Throws the error:
Initializer for conditional binding must have Optional type, not 'Image'
Solution 3
let image = Image(systemName: name)
if image != nil {
return image
}
Does not work and gives the warning:
Comparing non-optional value of type 'Image' to 'nil' always returns true
Aucun commentaire:
Enregistrer un commentaire