在 Swift 中,可以使用字符串的 isEmpty
属性来判断字符串是否为空,如下所示:
let str = ""
if(str.isEmpty) {
// 字符串为空
} else {
// 字符串不为空
}
也可以使用字符串的 count
属性来判断字符串长度是否为 0,如下所示:
let str = ""
if(str.count == 0) {
// 字符串为空
} else {
// 字符串不为空
}
两种方法都可以用来判断字符串是否为空,返回结果是一样的。