EXC_BREAKPOINT Crashlytics 中的 0x00000000e7ffdefe

EXC_BREAKPOINT 0x00000000e7ffdefe in Crashlytics

我在 Crashlytics 中收到以下错误

LoginViewController.swift line 277 LoginViewController.textField(UITextField, shouldChangeCharactersInRange : _NSRange, replacementString : String) -> Bool

我的项目中有代码:

   // MARK: - UITextFieldDelegate
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    if textField == txtSearch && string != "\n"{
        var substring: String = textField.text!
        substring = (substring as NSString).stringByReplacingCharactersInRange(range, withString: string)
        self.searchAutocompleteEntriesWithSubstring(substring)
    }
    return true;
}

这个错误只是偶尔出现

您正在强行展开您的 textField.text!,这可能是零。试试下面的代码:

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    if textField == txtSearch && string != "\n"{
        if let text = textField.text {
            let substring = (text as NSString).stringByReplacingCharactersInRange(range, withString: string)
            self.searchAutocompleteEntriesWithSubstring(substring)
        }
    }
    return true
}