大家好我在快速3 iphone程序上使用alamofire,我的问题是我需要从php页面返回一个值我的问题是返回给我的价值是这个。我如何确保我返回的值是:no prova@email.it
我希望我已经解释了
返回值(不正确):
import Foundation
import Alamofire
class User{
//URL to our web service
var email=""
var password=""
func PrintValue(){
// print(username);
//print(password);
}
func Login() -> String{
//var ris="";
var readvalue=""
let URLString = "http://localhost/test/login_mobile.php"
let parameters_value: Parameters = [
"email": email,
"password": password
]
//Sending http post request
Alamofire.request(URLString, method: .post, parameters: parameters_value).responseJSON
{
response in
//printing response
print(response)
//getting the json value from the server
if let result = response.result.value {
//converting it as NSDictionary
let jsonData = result as! NSDictionary
//displaying the message in label
readvalue = (jsonData.value(forKey: "message") as! String?)!
print(readvalue)
}
}
return readvalue
}
}
} 否可选(" prova@email.it")
SWIFT CODE:
<?php
include 'user.php';
header('Content-Type: application/json');
$email= $_POST['email'];
$password = $_POST['password'];
$ris['message']="";
$user = new User();
//procedo con il login
if($user->login($email,$password,"MOBILE")==true){
$ris['message']="yes";
}
else{
$ris['message']="no $email";
}
echo json_encode($ris);
?>
PHP代码:
{{1}}
答案 0 :(得分:0)
刚刚在操场上做到这一点,它按预期工作..
import UIKit
let response: [AnyHashable: Any] = [
"message": "hello world"
]
class MyCoolClass {
func login() {
var readvalue = ""
let jsonData = response as NSDictionary
readvalue = jsonData.value(forKey: "message") as! String
debugPrint("the readvalue is: \(readvalue)")
}
}
let instance = MyCoolClass()
instance.login()
这将打印:&#34; readvalue是:hello world&#34;
代码不是很安全......
答案 1 :(得分:0)
我认为可以这样做:
if let readvalue = jsonData.value(forKey: "message") as? String {
print(readvalue)
}
必须打印无选择
答案 2 :(得分:0)
您需要使用nil合并来使您的值不可选。例如print(readvalue ?? "nil")
。更好的方法是重新设计您的响应处理以正确返回类型值或特定错误。