Check empty string in Swift?

前端 未结 15 1807
臣服心动
臣服心动 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:19
    public extension Swift.Optional {
        
        func nonEmptyValue<T>(fallback: T) -> T {
            
            if let stringValue = self as? String, stringValue.isEmpty {
                return fallback
            }
            
            if let value = self as? T {
                return value
            } else {
                return fallback
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-28 04:22

    You can use this extension:

    extension String {
    
        static func isNilOrEmpty(string: String?) -> Bool {
            guard let value = string else { return true }
    
            return value.trimmingCharacters(in: .whitespaces).isEmpty
        }
    
    }
    

    and then use it like this:

    let isMyStringEmptyOrNil = String.isNilOrEmpty(string: myString)
    
    0 讨论(0)
  • 2020-11-28 04:27

    isEmpty will do as you think it will, if string == "", it'll return true. Some of the other answers point to a situation where you have an optional string.

    PLEASE use Optional Chaining!!!!

    If the string is not nil, isEmpty will be used, otherwise it will not.

    Below, the optionalString will NOT be set because the string is nil

    let optionalString: String? = nil
    if optionalString?.isEmpty == true {
         optionalString = "Lorem ipsum dolor sit amet"
    }
    

    Obviously you wouldn't use the above code. The gains come from JSON parsing or other such situations where you either have a value or not. This guarantees code will be run if there is a value.

    0 讨论(0)
  • 2020-11-28 04:27

    I can recommend add small extension to String or Array that looks like

    extension Collection {
        public var isNotEmpty: Bool {
            return !self.isEmpty
        }
    }
    

    With it you can write code that is easier to read. Compare this two lines

    if !someObject.someParam.someSubParam.someString.isEmpty {}
    

    and

    if someObject.someParam.someSubParam.someString.isNotEmpty {}
    

    It is easy to miss ! sign in the beginning of fist line.

    0 讨论(0)
  • 2020-11-28 04:29

    There is now the built in ability to detect empty string with .isEmpty:

    if emptyString.isEmpty {
        print("Nothing to see here")
    }
    

    Apple Pre-release documentation: "Strings and Characters".

    0 讨论(0)
  • 2020-11-28 04:37

    Here is how I check if string is blank. By 'blank' I mean a string that is either empty or contains only space/newline characters.

    struct MyString {
      static func blank(text: String) -> Bool {
        let trimmed = text.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
        return trimmed.isEmpty
      }
    }
    

    How to use:

    MyString.blank(" ") // true
    
    0 讨论(0)
提交回复
热议问题