Trouble Comparing String

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

  1. CNuland macrumors newbie

    Joined:
    Jun 17, 2010
    #1
    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]; 		
    	}
    
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    How are you printing its value to the console?
     
  3. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #3
    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

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

    Joined:
    Mar 30, 2008
    Location:
    Uppsala, Sweden
    #5
    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".
     

Share This Page