我通过iOS应用程序在我的服务器上运行php脚本,该应用程序检查输入到UITextFields中的详细信息并允许用户登录。从PHP脚本返回的响应只是
<?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);
?>
这个回显的数字作为NSData返回到我的应用程序中,并且在NSURLConnectionDataDelegate回调之一中,我将该数据转换为NSString,然后从该字符串中获取intValue。
- (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.
}
两个日志语句对于相同的数据具有不同的输出。
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
为什么在一个实例中为3,在另一个实例中为0,为什么在第一个日志语句中都添加了空格?
答案 0 :(得分:2)
字符串有一堆换行符;去除那些,你应该得到预期的intValue
答案 1 :(得分:0)
试试这个:
-(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;
}
答案 2 :(得分:0)
看到我需要的角色就在最后我只是使用
string =[string substringFromIndex:[string length]-1];
有效,但想知道空白的原因
编辑:按照奥斯汀的建议,我不得不删掉很多帖子 根据他的回答在评论中这样做的代码。