 |
 |
|
 |
11-16-2008, 08:53 PM
|
#1 (permalink)
|
|
Junior Member
Join Date: Nov 2008
Posts: 27
Rep Power: 0
|
code works on simulator and not device
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;
}
|
|
|
11-16-2008, 09:33 PM
|
#2 (permalink)
|
|
Senior Member
Join Date: Oct 2008
Location: Denver, CO
Posts: 2,121
Rep Power: 3
|
Yep, you really need to read the Objective-C docs. Besides all the memory leaks talked about in your other thread there are many other things wrong with this code, or at least things that can be done better.
The biggest thing that is causing your problem is the string comparisons. You can't compare two strings for equality by using the '==' operator. In all the C derived languages, among many others, the '==' operator compares if two objects are the same object, not the same value. The NSString class has the method 'isEqualToString:'. So change:
one_char == testp
to:
[one_char isEqualToString:testp]
And to create a string you are doing it in a very inefficient way. For example, instead of:
test4 = [[NSString alloc] initWithFormat:@"4"]
just do:
test4 = @"4";
This also eliminates one of your leaks.
That's enough for now. HTH.
|
|
|
11-16-2008, 09:43 PM
|
#3 (permalink)
|
|
Junior Member
Join Date: Nov 2008
Posts: 27
Rep Power: 0
|
Thank you very much for your help. I will do a little more learning regarding objective-C and implement your recommendations in the meantime.
Thanks
|
|
|
 |
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
» Advertisements |
|
» Online Users: 236 |
| 30 members and 206 guests |
| 2ndSegment, bondjpf, crossfire, dapis, davidlansalot, ElysianEagle, enfamus, exosyphen, freshking, g.castaldi, gbh, Groucho, hugo, joshsroka, Jume, kaleman, Lars, lastiko, lildragon, martinws, mattjgalloway, MetaImi, mnemonic_fx, nattylux, OneGlobe, Opticfibre, shabzcohelp, Sicga, StatCoder, williamlegate |
| Most users ever online was 779, 05-11-2009 at 09:55 AM. |
» Stats |
Members: 8,231
Threads: 20,199
Posts: 90,216
Top Poster: RickMaddy (2,121)
|
| Welcome to our newest member, OneGlobe |
|