duanji9481 2016-09-30 19:49
浏览 53
已采纳

PHP,Xcode 8,Swift 3 - 如何将数组从Web服务器返回到Xcode模拟器

I can read text from a web server and print it to the Xcode uilabel, but I don't know how to parse a returning array. I am trying to get a and b from the server, add them, and print to uilabel using Swift 3.

This is the PHP and Xcode 8:

connect-add.php

<?php
$a=3;
$b=5;
$data=array($a,$b);
echo json_encode($data);
?>

Xcode 8

import UIKit
import Foundation

class ViewController: UIViewController {

var a=0
var b=0
var c=0


@IBOutlet weak var result: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a 
nib.
}

@IBAction func getVars(_ sender: AnyObject) {

    // set label during request

    self.result.text="WAITING"

    // send request to connect-add.php and retrieve a and b

    let myUrl = NSURL(string: "http://www.example.com/connect-add.php")

    var request = URLRequest(url:URL(string: 
"http://www.example.com/connect-add.php")!)

    request.httpMethod = "POST"

    let postString = ""

    request.httpBody = postString.data(using: .utf8);

    let task = URLSession.shared.dataTask(with: request) {
        data, response, error in

        if error != nil
        {
            print("error=\(error)")
            return
        }

        // receive response to connect-add and parse to a and b

        var response = String(data: data!, encoding: .utf8)

        var a=response[0]
        var b=response[1]

        // add a and b and display

        var c=a+b
        self.result.text=c
    }
    task.resume()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}

Xcode gives me an error for setting var a=response[0] and for setting var b=response[1]. Any ideas?

I have editted PHP and Xcode to add JSON Serialization parsing. Xcode is showing one error--Use of unresolved identifier "JSON". Here is the code.

PHP

<?php
$a=3;
$b=5;
$data="a1=".$a.", b1=".$b;
echo json_encode($data);
?>

Xcode 8

import UIKit
import Foundation

class ViewController: UIViewController {

var a=0
var b=0
var c=0


@IBOutlet weak var result: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a 
nib.
}

@IBAction func getVars(_ sender: AnyObject) {

    // set label during request

    self.result.text="WAITING"

    // send request to connect-add.php and retrieve a and b

    let myUrl = NSURL(string: "http://www.example.com/connect-add.php")

    var request = URLRequest(url:URL(string: 
"http://www.example.com/connect-add.php")!)

    request.httpMethod = "POST"

    let postString = ""

    request.httpBody = postString.data(using: .utf8);

    let task = URLSession.shared.dataTask(with: request) {
        data, response, error in

        if error != nil
        {
            print("error=\(error)")
            return
        }

        // receive response to connect and parse to a and b

        var response = String(data: data!, encoding: .utf8)

        var obj=JSON.parse(response)

        var a=obj.a1
        var b=obj.b1

        // add a and b and display

        var c=a+b
        self.result.text=c
    }
    task.resume()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}
  • 写回答

1条回答 默认 最新

  • dongqian3750 2016-11-04 18:18
    关注
    if var json = try JSONSerialization.jsonObject(with: data) as? [String: 
    String], var a2 = json["a"], var b2 = json["b"] {
    // It's working
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题