【问题标题】:iOS Swift4 how to check if string is not nil and not empty? [duplicate]iOS Swift4如何检查字符串是否不为零且不为空? [复制]
【发布时间】:2018-05-31 13:23:23
【问题描述】:

如何在 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
    }
 }

【问题讨论】:

标签: ios string swift4


【解决方案1】:

我会选择类似的东西

if let title = object.title, !title.isEmpty {
  // it's not nil nor an empty string
}

【讨论】:

    猜你喜欢
    • 2011-04-05
    • 2019-01-25
    • 1970-01-01
    • 1970-01-01
    • 2011-01-23
    • 1970-01-01
    • 1970-01-01
    • 2010-09-20
    • 2020-05-29
    相关资源
    最近更新 更多