Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过PHP从mySQL到swift获取值_Php_Ios_Mysql_Swift - Fatal编程技术网

通过PHP从mySQL到swift获取值

通过PHP从mySQL到swift获取值,php,ios,mysql,swift,Php,Ios,Mysql,Swift,我有一个返回json_encode值的php文件,当我转到http地址时,它们会给我值,但是 我无法从服务器端获取我的应用程序的价值我尝试了很多次,但都没有得到 func loadData() { let url = NSURL(string: "http://example.com/getExpo.php") let request = NSMutableURLRequest(URL: url!) // modify the request as necessary

我有一个返回json_encode值的php文件,当我转到http地址时,它们会给我值,但是 我无法从服务器端获取我的应用程序的价值我尝试了很多次,但都没有得到

func loadData() {

    let url = NSURL(string: "http://example.com/getExpo.php")
    let request = NSMutableURLRequest(URL: url!)

    // modify the request as necessary, if necessary

    NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: { (data:NSData?, response:NSURLResponse?, error:NSError?) -> Void in

        if error != nil {
            // Display an alert message

            print(error)

            return
        }

        do {

            let json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary


                if (json != nil) {

                    //let userId = parseJSON["userId"] as? String

                    // Display an alert message
                    let userMessage = json!["id"] as? String

                    print(userMessage)

                } else {

                    // Display an alert message
                    let userMessage = "Could not fetch Value"
                    print(userMessage)

                }



        } catch  {

            print(error)

        }

    }).resume()


}
任何人都可以帮忙,谢谢

您的JSON响应是一个字典数组:

[{“id”:“115”,“expoName”:“aziz”,“expoDetails”:“aziz”,“expoPhone”:“aziz”,“expoLocation”:“aziz”}]

但你想把它变成一本字典:

let json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary
当然,解决方案是将其转换为数组:

let json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as? NSArray
如果可以,最好使用Swift类型:

let json = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? [[String:AnyObject]]
例如,您可以使用循环:

if let json = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? [[String:AnyObject]] {
    for item in json {
        let userMessage = item["id"] as? String
    }
}
JSON响应是一组字典:

[{“id”:“115”,“expoName”:“aziz”,“expoDetails”:“aziz”,“expoPhone”:“aziz”,“expoLocation”:“aziz”}]

但你想把它变成一本字典:

let json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary
当然,解决方案是将其转换为数组:

let json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as? NSArray
如果可以,最好使用Swift类型:

let json = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? [[String:AnyObject]]
例如,您可以使用循环:

if let json = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? [[String:AnyObject]] {
    for item in json {
        let userMessage = item["id"] as? String
    }
}

请提供您的URl并发布或获取数据,我会检查。请提供您的URl并发布或获取数据,我会检查。如果可以,请立即将值返回到集合视图。如果可以,请立即将值返回到集合视图