Check If Custom Class Not Nil --- Swift Is Converting It To A UInt8

Aggressor

enter image description here

I have a custom image class and I am just trying to check if its not nil.

Every syntax I try (with the above being my best guess) keeps saying my iImage is not a UInt8.

First: why is it thinking by comparing to nil its a UInt8 in the first place?

Second: how can I check if my custom class is nil?

Update:

Answers are appreciated but didn't work. Here is full code block

private class func doubleLayoutView(mainView:UIView,images:Array<iImage>)
    {
        for i in 0...1{

            var image:iImage = images[i]
            if image.uiImage!=nil
            {
                image.imageFrame = CGRectMake(CGFloat(i)*mainView.frame.width/2, 0, mainView.frame.width/2, mainView.frame.height)
            }
        }
    }
Maxim Shoustin

I have a custom image class and I am just trying to check if its not nil

Be sure that iamge.uiImage is Optional. Otherwise you can't compare it to nil

In this case Swift helps you do avoid extra validation != nil.

On other hand if uiImage is not Optional (a.e. marked without ?) means that this value is not nil and you can remove if validation statement because it doesn't make sense

how can I check if my custom class is nil?

If you feel pretty confident that custom class is not nil - don't check it. If it might be nil - define it as Optional like:

var uiImage:UIImage?

In this case each time when you are going to use it, need to unwrap it 1st

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related