下面的代码删除了inputField中的最后一个字符,当我有默认值时,它工作得非常好。但如果文本字段为空,则会出现错误,因为没有要删除的最后一个字符。
如何与if else核对?

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
    textField.text?.removeLast()
    percentageField.text = textField.text
    return formViewController()?.textInputShouldBeginEditing(textField, cell: self) ?? true
}

最佳答案

removeLast必须用于非空字符串,因此请确保它不是空的:

if let text = textField.text, !text.isEmpty {
    textField.text?.removeLast()
}

关于ios - Swift:textFieldShouldBeginEditing功能修复,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51299371/

10-15 07:24