如何检查可选字符串对象在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)
我会喜欢这样的东西
if let title = object.title, !title.isEmpty {
// it's not nil nor an empty string
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2738 次 |
| 最近记录: |