Check empty string in Swift?

2020-01-27 00:23发布

In Objective C, one could do the following to check for strings:

if ([myString isEqualToString:@""]) {
    NSLog(@"myString IS empty!");
} else {
    NSLog(@"myString IS NOT empty, it is: %@", myString);
}

How does one detect empty strings in Swift?

标签: swift
13条回答
我欲成王,谁敢阻挡
2楼-- · 2020-01-27 01:11

A concise way to check if the string is nil or empty would be:

var myString: String? = nil

if (myString ?? "").isEmpty {
    print("String is nil or empty")
}
查看更多
登录 后发表回答