I am running a php script on my server via an iOS app that checks details entered into UITextFields and allows user to log in. Th response back from the PHP script is simply

   <?php require_once("includes/connection_auth.php"); 
?>
<?php


    $username = $_GET["userName"];
    $userpassword = $_GET["userPassword"];

    //check if users username exists in database
    $selectQuery = mysqli_query($connection, "SELECT * FROM MVUsers WHERE userName = '$username' ");
    if (!$selectQuery) {
        printf("Error: %s\n", mysqli_error($connection));
        exit();
    }


    if ($row = mysqli_fetch_assoc($selectQuery)){
        //user found in database check password matches



         if($row ['userPassword'] == $userpassword){
            //password matches check if confirmed email
                if($row ['userHasConfirmedEmail'] == 1){
                     //allow user to log in
                    echo (0);
                  }
                  else{
                  echo (1);

                 }

          }
          else{
            //password does not match warn user

            echo (2);
               }    

    }



    else{

    echo(3);

   }
    //close connection to database
    mysqli_close($connection);

?>

This echoed number is returned as NSData into my app and inside one of the NSURLConnectionDataDelegate callbacks i convert that data to an NSString then get the intValue from that string.

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *string = [[NSString alloc]initWithData:[self receivedData] encoding:NSUTF8StringEncoding];

NSLog(@"Connection finished loading with string :%@",string);

int returnedInt = [string intValue];
 NSLog(@"%i",returnedInt);

// returnedInt is the compared inside a switch statement. 
}

The two log statements have different outputs for same data.

2014-02-05 13:20:47.363 MotoVlogger[50944:70b] Connection finished loading with string :






3
2014-02-05 13:20:47.363 MotoVlogger[50944:70b] 0

Why is is 3 in one instance and 0 in the other also why is all there all that added white space in the first log statement?

有帮助吗?

解决方案

The string has a bunch of newlines; strip those out and you should get the expected intValue

其他提示

try this:

-(NSInteger)convert:(NSString *)str
{
    str =[str stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSInteger returnedInt = [str integerValue];
    NSLog(@"%ld",(long)returnedInt);
    return (long)returnedInt;
}


-(int)convert:(NSString *)str
{
    str =[str stringByReplacingOccurrencesOfString:@" " withString:@""];
    int returnedInt = [str intValue];
    NSLog(@"%i",returnedInt);
    return returnedInt;
}

seeing as the character i need is right at the end I'm just using

string =[string substringFromIndex:[string length]-1];

works but would like to know cause of white space

edit: as suggested by Austin I had to strip out lots so posted code to do so in comment under his answer.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top