iOS Swift4如何检查string是否为nil并且不为空?

Ale*_*one 3 string ios swift4

如何检查可选字符串对象在Swift4中是否既不是空字符串“”也不是nil?我最终不得不写这些奇怪的支票,因为

 //object has instance variable     
 var title: String?

 //invalid comparison - cannot compare optional and non optional
 if object.title?.count > 0
 {

 }

 //valid but ugly
 if object.titleString == nil {
      //has nil title
 }
 if let title = object.title
 {
    if title.count == 0
    {
        //has a "" string
    }
 }
Run Code Online (Sandbox Code Playgroud)

pst*_*ued 6

我会喜欢这样的东西

if let title = object.title, !title.isEmpty {
  // it's not nil nor an empty string
}
Run Code Online (Sandbox Code Playgroud)