0
LoginService
クラスを作成して、メソッドPOSTを使用してWebサービスからデータを取得し、このクラスをUIViewController
で呼び出します。それは機能しますが、変数loginObj.fullname
の値を別のViewController
に表示したいとします。私は非常に多くの方法を試しましたが、私が持っている出力は空の文字列です。私は何をすればいいのですか?ここに私のLoginService
クラスコードです:他のviewControllerで変数を使用するには? - Swift
func callService(_ urlString: String, callback: @escaping(_ LoginArray:[LoginModel]) -> Void, username:String, password:String)
{
let url = URL(string: urlString);
var request = URLRequest(url: url!);
request.httpMethod = "POST";
let postString = "username=" + username + "&password=" + password;
request.httpBody = postString.data(using: .utf8);
let sessionConfig = URLSessionConfiguration.ephemeral;
let session = URLSession(configuration: sessionConfig);
let task = session.dataTask(with: request)
{
(data:Data?, URLResponse:URLResponse?, error:Error?) in
if error != nil
{
print("\n\n\n\n\n\(error?.localizedDescription)\n\n\n\n\n");
}
else
{
if let returnData = data
{
do
{
var valSucceeded = false;
var valResponseCode = "";
var valResponseDescription = "";
var valFullname = "";
var valToken = "";
var LoginArray = [LoginModel]();
print("\n\n\n\n\n");
if let jsonObject = try JSONSerialization.jsonObject(with: returnData, options: .allowFragments) as? [String: AnyObject]
{
valSucceeded = (jsonObject["succeeded"] as? Bool)!;
valResponseCode = (jsonObject["responseCode"] as? String)!;
valResponseDescription = (jsonObject["responseDescription"] as? String)!;
valFullname = (jsonObject["fullname"] as? String)!;
valToken = (jsonObject["token"] as? String)!;
let entryResult = LoginModel(succeeded: valSucceeded, responseCode: valResponseCode, responseDescription: valResponseDescription, fullname: valFullname, token: valToken);
LoginArray.append(entryResult);
}
DispatchQueue.main.async(execute: {
callback(LoginArray);
});
print("\n\n\n\n\n");
}
catch
{
print("\n\n\n\n\n JSON Serialization Error \n\n\n\n\n");
}
}
else
{
print("\n\n\n\n\n Call API Error \n\n\n\n\n");
}
}
}
task.resume();
}
そして、これは私のViewControllerコードです:
@IBAction func loginClick(_ sender: AnyObject) {
let input_username = username.text;
let input_password = password.text;
if ReachAbility.isConnectedToNetwork() == true
{
let service = LoginServices();
service.callService("http://www........", callback: callServiceFinished, username: input_username!, password: input_password!);
}
else
{
let failedStatus = "ไม่สามารถเชื่อมต่ออินเทอร์เนตได้";
let alert = UIAlertController(title: failedStatus, message: "กรุณาตรวจสอบการเชื่อมต่อ", preferredStyle: .alert);
let msgAlert = UIAlertAction(title: "ตกลง", style: .cancel, handler: nil);
alert.addAction(msgAlert);
present(alert, animated: true, completion: nil);
}
}
func callServiceFinished(_ LoginArray:[LoginModel]) {
print("\n\n\n\n\n");
for loginObj in LoginArray
{
print("\(loginObj.succeeded)\n");
print("\(loginObj.responseCode)\n");
print("\(loginObj.responseDescription)\n");
print("\(loginObj.fullname)\n");
print("\(loginObj.token)");
print("\n\n");
if (loginObj.succeeded != true)
{
let alert = UIAlertController(title: nil, message: loginObj.responseDescription, preferredStyle: .alert);
let msgAlert = UIAlertAction(title: "OK", style: .cancel, handler: nil);
alert.addAction(msgAlert);
present(alert, animated: true, completion: nil);
}
else
{
print("Error!")
}
}
print("\n\n\n\n\n");
}
valFullnameとfullnameはどちらも同じですか? –
はい、両方とも同じ値です –
データを印刷しましたか( "\(loginObj.succeeded)\ n"); print( "\(loginObj.responseCode)\ n"); print( "\(loginObj.responseDescription)\ n"); print( "\(loginObj.fullname))\ n"); print( "\(loginObj.token)"); –