Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

CNuland

macrumors newbie
Original poster
Jun 17, 2010
18
0
I have a string that is set to a HTTP response. When I print the value to the console, it tells me it is -1. However, it is not working when I compare the string to @"-1". Is it possible that there is hidden information being encoded from the HTTP response?

Code:
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
	NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
	
	
	if([returnString isEqualToString:@"-1"]){
		NSLog(@"works");
			UIAlertView *alert = [[UIAlertView alloc] 
								  initWithTitle:nil message:@"That username is already in use." 
								  delegate:self cancelButtonTitle:@"OK" 
								  otherButtonTitles:nil]; 
		[alert show]; 
		[alert release]; 		
	}
 
Obviously there could be a blank before or after the text that you show, which might not be visible when you print it. You can inspect the length of the string. When you print it you can print quotes before and after the string to see if there are any blanks. You can use rangeOfString to see if the -1 string is contained inside your result.
 
Thanks for the help, I got it working by using if ([returnString rangeOfString:mad:"-"].location == 0).
 
If you get the result string from an HTTP connection you probably have a CR/LF in there. You can try cleaning the string using this:

Code:
NSString *cleanString = [returnString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

and then comparing to "-1".
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.