Sandrika Janjghava
Sandrika Janjghava

Reputation: 59

filter String with empty String in if statement, iOS

I'm working on a custom keyboard. I'm having trouble filtering Strings Array. I want to filter the array only in if statement, I have an array with letters and empty String's. I want to filter Array only in if statement because I want to remove last character, not empty String.

Here is my code:

keyboardKeyStr = ["h", "e", "l", "l", "o", " ", "", "t", "h", "i", "s", "", "i", "s", " ", "m", "y", " ", "", "a", "r", "r", "a", "y", " "]

if range.length == 1 && text.count == 0 {
    print("Delete Button")

    let emptyStr: String? = ""
    keyboardKeyStr = keyboardKeyStr.filter {$0 != emptyStr}
    keyboardKeyStr.removeLast()
    keyboardKeyArr.append(keyboardView.deleteBtn)
    self.keyboardKeyStr.append("")
}

Upvotes: 3

Views: 475

Answers (1)

Tiago Couto
Tiago Couto

Reputation: 351

Try this keyboardKeyStr = keyboardKeyStr.filter{ $0 != "" && $0 != " "}

Upvotes: 1

Related Questions