I have the following code on Swift
var image = UIImage(contentsOfFile: filePath) if image != nil { return image }
It used to work great, but now on Xcode Beta 6, this returns a warning
'UIImage' is not a subtype of 'NSString'
I don't know what to do, I tried different things like
if let image = UIImage(contentsOfFile: filePath) { return image }
But the error changes to:
Bound value in a conditional binding must be of Optional type
Is this a bug on Xcode6 beta 6 or am I doing something wrong?
Update
Swift now added the concept of failable initializers and UIImage is now one of them. The initializer returns an Optional so if the image cannot be created it will return nil.
Variables by default cannot be nil
. That is why you are getting an error when trying to compare image
to nil
. You need to explicitly define your variable as optional:
let image: UIImage? = UIImage(contentsOfFile: filePath) if image != nil { return image! }
The simplest way to check if an image has content (> nil) is:
if image.size.width != 0 { do someting}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With