PHP, Xcode 8, Swift 3--如何return一个数组从web服务器到Xcode模拟器

PHP, Xcode 8, Swift 3--How to return an array from web server to Xcode simulator

我可以从网络服务器读取文本并将其打印到 Xcode uilabel,但我不知道如何解析返回的数组。我正在尝试从服务器获取 a 和 b,添加它们,然后使用 Swift 打印到 uilabel 3.

这是 PHP 和 Xcode 8:

连接-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 给我设置 var a=response[0] 和设置 var b=response[1] 的错误。有什么想法吗?

我编辑了 PHP 和 Xcode 以添加 JSON 序列化解析。 Xcode 显示一个错误--使用未解析的标识符 "JSON"。这是代码。

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.
}
}
if var json = try JSONSerialization.jsonObject(with: data) as? [String: 
String], var a2 = json["a"], var b2 = json["b"] {
// It's working

}