退格键和输入新字符后 NSRange `replaceCharactersInRange` 应用程序崩溃

NSRange `replaceCharactersInRange` crashing app after backspace & entry of new character

我有一个文本框,&使用shouldChangeCharactersInRange方法获取值。

这是我的代码:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

    NSLog(@"Pressing : %@\n", string);
    NSMutableString *text = [NSMutableString stringWithString:textField.text];

    NSLog(@"length: %lu", (unsigned long)range.length);
    NSLog(@"location: %lu\n", (unsigned long)range.location);
    NSLog(@"before text: %@", textField.text);

    [text replaceCharactersInRange:range withString:string];
    NSLog(@"After replacement , Text: %@\n\n", text);

    // My work..

    return YES;
}

我首先将字符 'T' 添加到文本字段,它工作正常..

2016-03-02 10:06:23.162 MyTextFieldApp[12006:4323645] Pressing : T
2016-03-02 10:06:23.163 MyTextFieldApp[12006:4323645] length: 0
2016-03-02 10:06:23.163 MyTextFieldApp[12006:4323645] location: 0
2016-03-02 10:06:23.163 MyTextFieldApp[12006:4323645] before text: 
2016-03-02 10:06:23.163 MyTextFieldApp[12006:4323645] After replacement , Text: T

然后,我将其退格以删除字符,这也很好..

2016-03-02 10:06:30.979 MyTextFieldApp[12006:4323645] Pressing : 
2016-03-02 10:06:30.980 MyTextFieldApp[12006:4323645] length: 1
2016-03-02 10:06:30.980 MyTextFieldApp[12006:4323645] location: 0
2016-03-02 10:06:30.980 MyTextFieldApp[12006:4323645] before text: T
2016-03-02 10:06:30.981 MyTextFieldApp[12006:4323645] After replacement , Text: 

但是,当我要添加新角色时,它在 replaceString 上崩溃了。

Note: This time, Range Gives length 0 & location 1, while first time also when i am adding T to empty textfield, it gives length 0 as well as location 0.

2016-03-02 10:07:02.158 MyTextFieldApp[12006:4323645] Pressing : T
2016-03-02 10:07:02.158 MyTextFieldApp[12006:4323645] length: 0
2016-03-02 10:07:02.159 MyTextFieldApp[12006:4323645] location: 1
2016-03-02 10:07:02.159 MyTextFieldApp[12006:4323645] before text: 
2016-03-02 10:07:02.159 MyTextFieldApp[12006:4323645] *** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFString replaceCharactersInRange:withString:]: Range or index out of bounds'

我无法检测到问题出在哪里?

您的程序正在崩溃,因为您试图在 text 可变字符串中的无效位置插入数据。

第二次按 'T' 时,text 的值(从 textField.text 检索)是一个空字符串(字面意思是 @"")。然后,您调用 replaceCharactersInRange 传递 location 1 和 length 0。当您调用 replaceCharactersInRange 并使用 length 0 时,您实际上是在说 "insert string" 在位置 location。此函数仅在 length 大于 0 时替换文本。

当您的代码尝试在索引 1 处插入字母 'T' 时,它无法插入(空字符串的第一个也是唯一有效索引为 0),因此程序崩溃。

此外,就其价值而言,如果 textField 为空,您的程序不应 return 位置 1 和长度 0。一定有别的事情发生了。我在 Swift 中编写了您的示例程序并得到以下输出:

import UIKit

class ViewController: UIViewController {
    var textField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.blackColor()

        textField = UITextField(frame: CGRectMake(20, 100, 200, 20))
        textField.backgroundColor = UIColor.whiteColor()
        textField.delegate = self

        view.addSubview(textField)
    }
}


extension ViewController: UITextFieldDelegate {
    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
        print("range.length: \(range.length), range.location: \(range.location), text: '\(textField.text!)' string: '\(string)'")
        return true
    }
}

输出:

range.length: 0, range.location: 0, text: '' string: 'T' // press T
range.length: 1, range.location: 0, text: 'T' string: '' // press backspace
range.length: 0, range.location: 0, text: '' string: 'T' // press T

如您所见,我在第二次插入 T 时看到了 0,0 的预期范围。Objective-C 程序的行为应该类似。