Trouble Comparing String

Discussion in 'iOS Programming' started by CNuland, Jun 17, 2010.

  1. CNuland macrumors newbie

    Jun 17, 2010
    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?

    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    	NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
    	if([returnString isEqualToString:@"-1"]){
    			UIAlertView *alert = [[UIAlertView alloc] 
    								  initWithTitle:nil message:@"That username is already in use." 
    								  delegate:self cancelButtonTitle:@"OK" 
    		[alert show]; 
    		[alert release]; 		
  2. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    How are you printing its value to the console?
  3. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    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.
  4. CNuland thread starter macrumors newbie

    Jun 17, 2010
    Thanks for the help, I got it working by using if ([returnString rangeOfString:mad:"-"].location == 0).
  5. bredell macrumors regular

    Mar 30, 2008
    Uppsala, Sweden
    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:

    NSString *cleanString = [returnString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    and then comparing to "-1".

Share This Page