我正在使用自定义键盘。我在过滤字符串数组时遇到问题。我想只在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("")
}
答案 0 :(得分:0)
试试这个keyboardKeyStr = keyboardKeyStr.filter{ $0 != "" && $0 != " "}