I have written some code that will parse a text field and replace it with only the numeric values that were typed in. For example if the user types in: 200,000 I will parse it and set the value in the field to 200000. The code works perfectly in the simulator, but on the device it fails. When I debug on the device I can see that the if statement in the code never gets triggered. This is very strange because it will get triggered on the simulator. I have supplied the code below in case it helps. Please let me know if you have any recommendations.
Code:
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
[theTextField resignFirstResponder];
//fix any non-numeric numbers
NSString *temp =nil;
NSString *new_string = nil, *one_char = nil, *testp = nil, *test0 = nil, *test1 = nil, *test2 = nil, *test3 = nil, *test4 = nil, *test5 = nil, *test6 = nil, *test7 = nil, *test8 = nil, *test9 = nil;
testp = [[NSString alloc] initWithFormat:@"."];
test0 = [[NSString alloc] initWithFormat:@"0"];
test1 = [[NSString alloc] initWithFormat:@"1"];
test2 = [[NSString alloc] initWithFormat:@"2"];
test3 = [[NSString alloc] initWithFormat:@"3"];
test4 = [[NSString alloc] initWithFormat:@"4"];
test5 = [[NSString alloc] initWithFormat:@"5"];
test6 = [[NSString alloc] initWithFormat:@"6"];
test7 = [[NSString alloc] initWithFormat:@"7"];
test8 = [[NSString alloc] initWithFormat:@"8"];
test9 = [[NSString alloc] initWithFormat:@"9"];
int string_length;
//new_string seems to be using the same address as one_char and new_stirng isn't being updated in the if statement
temp = [[NSString alloc] initWithFormat:@"%@", theTextField.text];
new_string = [[NSString alloc] initWithFormat:@""];
string_length = [temp length];
one_char = [[NSString alloc] initWithFormat:@""];
for(int i = 0; i < string_length; i=i+1)
{
one_char = [temp substringWithRange: NSMakeRange(i, 1)];
if (one_char == testp || one_char == test0 || one_char == test1 || one_char == test2 || one_char == test3 || one_char == test4 || one_char == test5 || one_char == test6 || one_char == test7 || one_char == test8 || one_char == test9)
{
new_string = [new_string stringByAppendingString:one_char];
}
}
theTextField.text = new_string;
return YES;
}