if statement - how to give nil parameter in if condition in swift? -
in objective-c:
if (!myimageview) { nslog(@"hdhd"); } else { //do }
but in swift:
if (!myimageview) { println("somethin") } else { println("somethin") }
this code giving me error:
could not find overload '!' accepts supplied arguments'
myimageview
class variable uiimageview
.
what should do?
usually, best way deal checking variables nil
in swift going if let
or if var
syntax.
if let imageview = self.imageview { // self.imageview not nil // can access through imageview } else { // self.imageview nil }
but work (or comparison against nil
either == nil
or != nil
), self.imageview
must optional (implicitly unwrapped or otherwise).
non-optionals can not nil
, , therefore compiler not let compare them against nil
. they'll never nil
.
so if if let imageview = self.imageview
or self.imageview != nil
or self.imageview == nil
giving errors, it's because self.imageview
not optional.
Comments
Post a Comment