在if语句,iOS中使用空字符串过滤字符串

时间:2018-03-01 15:40:33

标签: arrays swift string filter

我正在使用自定义键盘。我在过滤字符串数组时遇到问题。我想只在if语句中过滤数组,我有一个带字母和空字符串的数组。我想仅在if语句中过滤数组,因为我想删除最后一个字符,而不是空字符串。

这是我的代码:

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("")
}

1 个答案:

答案 0 :(得分:0)

试试这个keyboardKeyStr = keyboardKeyStr.filter{ $0 != "" && $0 != " "}