3

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("")
}
3
  • Your question is a bit confusing. You should add an example array and the expected result. By the way you can replace text.count == 0 with text.isEmpty and filter {$0 != emptyStr} with filter {!$0.isEmpty} and delete the let emptyStr line. And why do you declare emptyStr as an optional but assign an non-optional value?? Don't annotate worse types than the compiler can infer. Commented Mar 1, 2018 at 15:52
  • 1
    I have edited, the array looks like that Commented Mar 1, 2018 at 15:56
  • @SandrikaJanjghava You did not include the expected result. (What result are you receiving, and what did you want it to be, and why?) Commented Mar 1, 2018 at 18:43

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

filter is finding and removing, deleting. I found filterMap and that does great work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.