Check string for nil & empty

后端 未结 23 1323
感动是毒
感动是毒 2020-12-07 10:53

Is there a way to check strings for nil and \"\" in Swift? In Rails, I can use blank() to check.

I currently have this, but i

23条回答
  •  失恋的感觉
    2020-12-07 11:31

    You should do something like this:
    if !(string?.isEmpty ?? true) { //Not nil nor empty }

    Nil coalescing operator checks if the optional is not nil, in case it is not nil it then checks its property, in this case isEmpty. Because this optional can be nil you provide a default value which will be used when your optional is nil.

提交回复
热议问题