// self.wordTextField.text = @"32";
// self.wordTextField.text = [self.wordTextField.text lowercaseString];
// self.wordTextField.text = [self.wordTextField.text stringByReplacingOccurrencesOfString:@" " withString:@"?"];
NSString *removeString = @"`1234567890-=~!@#$%^&*()_+[]\\{}|;':\",./<>";
NSMutableSet *removeSet = [NSMutableSet set];
for (unsigned i = 0; i < removeString.length; i++) {
NSRange range; range.location = i; range.length = 1;
NSString *char_ = [removeString substringWithRange:range];
[removeSet addObject:char_];
}
for (unsigned i = 0; i < self.wordTextField.text.length; i++) {
NSRange range; range.location = i; range.length = 1;
NSString *thisLetter = [self.wordTextField.text substringWithRange:range];
if ([removeSet containsObject:thisLetter]) {
self.wordTextField.text = [self.wordTextField.text stringByReplacingOccurrencesOfString:thisLetter
withString:@""];
i--;
}
}
The three commented lines all cause my method to enter an infinite loop, because self.wordTextField.text is inside a method that is called when editing is being changed, so obviously my setting the text property causes more editing to change, resulting in the loop. My question is: why doesn't the .text setter within the loop and conditional statements cause this behavior? That part works perfectly as (a user) would expect...
三条注释行都导致我的方法进入无限循环,因为self.wordTextField.text位于更改编辑时调用的方法内部,因此显然我设置text属性会导致更多编辑更改,从而导致循环。我的问题是:为什么循环和条件语句中的.text setter不会导致这种行为?该部分完美地工作(用户)期望...
1 个解决方案
#1
1
The code again enters into a loop by calling your change callback multiple times but that is not an infinite loop because it eventually stops when all invalid characters are removed from the text. Note that, the setter inside the loop is not called if the text does not contain any characters to remove.
代码通过多次调用您的更改回调再次进入循环,但这不是无限循环,因为它最终会在从文本中删除所有无效字符时停止。请注意,如果文本不包含要删除的任何字符,则不会调用循环内的setter。
#1
1
The code again enters into a loop by calling your change callback multiple times but that is not an infinite loop because it eventually stops when all invalid characters are removed from the text. Note that, the setter inside the loop is not called if the text does not contain any characters to remove.
代码通过多次调用您的更改回调再次进入循环,但这不是无限循环,因为它最终会在从文本中删除所有无效字符时停止。请注意,如果文本不包含要删除的任何字符,则不会调用循环内的setter。