正如标题所述,我有一个UITextField
并使用textField:shouldChangeCharactersInRange:replacementString:
委托方法。
我收到了用户的崩溃日志,因为replacementString
中传递的字符串参数是nil
。当然,我可以简单地进行nil
检查以避免崩溃,但我想知道为什么字符串首先是nil
。
有人能想到字符串为nil
的原因吗?我试图删除字符,但所有人都无法重现这个案例。
如果这可能有所帮助,用户主要是日语。我认为这可能是一个unicode问题,但没有成功。
有关崩溃日志的信息:
nil
:https://developer.apple.com/documentation/uikit/uitextfielddelegate/1619599-textfield -> description: *** -[__NSCFString stringByReplacingCharactersInRange:withString:]: nil argument
。
实际代码如下所示:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *text = [textField.text stringByReplacingCharactersInRange:range
withString:string];
... some other code...
return YES;
}