###前提・実現したいこと
PHPのAPIからjsonデータを取得して、ボタンを押したら出力させたい。
###発生している問題・エラーメッセージ
myBottun関数内でapiGetterを呼び出し、戻り値を取得したのですが、varで宣言したdeviceType, channel, valueが初期値のままになってしまいます。
初歩的な質問で申し訳ありませんがよろしくお願いいたします。
エラーなし
###該当のソースコード
swift
1class ViewController: UIViewController { 2 3 func apiGetter(d_value:String, c_value:String, v_value:Int) -> (deviceType:String, channel:String, value:String) { 4 var deviceType = "" 5 var channel = "" 6 var value = "" 7 8 Alamofire.request("http://localhost:8080/ir_send.php", parameters: ["device_type": d_value, "channel": c_value, "value": v_value]) 9 .responseJSON { response in 10 guard let object = response.result.value else { 11 return 12 } 13 let json = JSON(object) 14 deviceType = json["result"][0]["device_type"].stringValue 15 channel = json["result"][0]["channel"].stringValue 16 value = json["result"][0]["value"].stringValue 17 print(deviceType) 18 } 19 print(deviceType) 20 return (deviceType, channel, value) 21 22 } 23 24 @IBAction func myBottun(_ sender: AnyObject) { 25 let res = apiGetter(d_value: "air", c_value: "cool_value", v_value: 18) 26 print(res.deviceType) 27 print(res.channel) 28 print(res.value) 29 } 30
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。