Check empty string in Swift?

前端 未结 15 1968
臣服心动
臣服心动 2020-11-28 03:51

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

if ([myString isEqualToString:@\"\"]) {
    NSLog(@\"m         


        
15条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-28 04:13

    In Xcode 11.3 swift 5.2 and later

    Use

    var isEmpty: Bool { get } 
    

    Example

    let lang = "Swift 5"
    
    if lang.isEmpty {
       print("Empty string")
    }
    

    If you want to ignore white spaces

    if lang.trimmingCharacters(in: .whitespaces).isEmpty {
       print("Empty string")
    }
    

提交回复
热议问题