Home News Forum Social Networking Support Us Advertise

Spanish Lesson 1 ($1.99)

aWake!Gently ($1.99)

The Bird & The Snail - Knock Knock - Deluxe ($4.99)

Match-It Trains ($0.99)

Tangled ($0.99)

iFlatter ($0.99)

The 15 puzzle ($0.99)

Tap Forms Database ($8.99)

Higher or Lower Card Game (Hi Lo) ($0.99)

Red Pixel ($0.99)

Time-Shift Radio ($0.99)

Want your application advertised here? Only $10/week!

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 11-16-2008, 08:53 PM   #1 (permalink)
Junior Member
 
Join Date: Nov 2008
Posts: 27
Rep Power: 0
mking is on a distinguished road
Default 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;
}
mking is offline   Reply With Quote
Old 11-16-2008, 09:33 PM   #2 (permalink)
Senior Member
 
RickMaddy's Avatar
 
Join Date: Oct 2008
Location: Denver, CO
Posts: 2,121
Rep Power: 3
RickMaddy is on a distinguished road
Default

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.
RickMaddy is offline   Reply With Quote
Old 11-16-2008, 09:43 PM   #3 (permalink)
Junior Member
 
Join Date: Nov 2008
Posts: 27
Rep Power: 0
mking is on a distinguished road
Default

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
mking is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


» Advertisements


» Stats
Members: 8,231
Threads: 20,199
Posts: 90,216
Top Poster: RickMaddy (2,121)
Welcome to our newest member, OneGlobe
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:36 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0