Check string for nil & empty

后端 未结 23 1235
感动是毒
感动是毒 2020-12-07 10:53

Is there a way to check strings for nil and \"\" in Swift? In Rails, I can use blank() to check.

I currently have this, but i

相关标签:
23条回答
  • 2020-12-07 11:07

    Using the guard statement

    I was using Swift for a while before I learned about the guard statement. Now I am a big fan. It is used similarly to the if statement, but it allows for early return and just makes for much cleaner code in general.

    To use guard when checking to make sure that a string is neither nil nor empty, you can do the following:

    let myOptionalString: String? = nil
    
    guard let myString = myOptionalString, !myString.isEmpty else {
        print("String is nil or empty.")
        return // or break, continue, throw
    }
    
    /// myString is neither nil nor empty (if this point is reached)
    print(myString)
    

    This unwraps the optional string and checks that it isn't empty all at once. If it is nil (or empty), then you return from your function (or loop) immediately and everything after it is ignored. But if the guard statement passes, then you can safely use your unwrapped string.

    See Also

    • Statements documentation
    • The Guard Statement in Swift 2
    0 讨论(0)
  • 2020-12-07 11:07

    you can use this func

     class func stringIsNilOrEmpty(aString: String) -> Bool { return (aString).isEmpty }
    
    0 讨论(0)
  • 2020-12-07 11:08

    Using isEmpty

    "Hello".isEmpty  // false
    "".isEmpty       // true
    

    Using allSatisfy

    extension String {
      var isBlank: Bool {
        return allSatisfy({ $0.isWhitespace })
      }
    }
    
    "Hello".isBlank        // false
    "".isBlank             // true
    

    Using optional String

    extension Optional where Wrapped == String {
      var isBlank: Bool {
        return self?.isBlank ?? true
      }
    }
    
    var title: String? = nil
    title.isBlank            // true
    title = ""               
    title.isBlank            // true
    

    Reference : https://useyourloaf.com/blog/empty-strings-in-swift/

    0 讨论(0)
  • 2020-12-07 11:10

    Swift 3 solution Use the optional unwrapped value and check against the boolean.

    if (string?.isempty == true) {
        // Perform action
    }
    
    0 讨论(0)
  • 2020-12-07 11:11

    If you are using Swift 2, here is an example my colleague came up with, which adds isNilOrEmpty property on optional Strings:

    protocol OptionalString {}
    extension String: OptionalString {}
    
    extension Optional where Wrapped: OptionalString {
        var isNilOrEmpty: Bool {
            return ((self as? String) ?? "").isEmpty
        }
    }
    

    You can then use isNilOrEmpty on the optional string itself

    func testNilOrEmpty() {
        let nilString:String? = nil
        XCTAssertTrue(nilString.isNilOrEmpty)
    
        let emptyString:String? = ""
        XCTAssertTrue(emptyString.isNilOrEmpty)
    
        let someText:String? = "lorem"
        XCTAssertFalse(someText.isNilOrEmpty)
    }
    
    0 讨论(0)
  • 2020-12-07 11:11

    In my opinion, the best way to check the nil and empty string is to take the string count.

    var nilString : String?
    print(nilString.count) // count is nil
    
    var emptyString = ""
    print(emptyString.count) // count is 0
    
    // combine both conditions for optional string variable
    if string?.count == nil || string?.count == 0 {
       print("Your string is either empty or nil")
    }
    
    0 讨论(0)
提交回复
热议问题