// 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…
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.