Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.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/4/json/13.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
Ios 如何使用闭包从NSURLSession返回JSON值_Ios_Json_Swift - Fatal编程技术网

Ios 如何使用闭包从NSURLSession返回JSON值

Ios 如何使用闭包从NSURLSession返回JSON值,ios,json,swift,Ios,Json,Swift,我试图从JSON对象返回一个结果,但无法返回。我是斯威夫特的新手,请告诉我怎么做。在下面的代码中,我想在函数fetchNumberOfSections()的返回中返回json\u level\u number,其中我现在硬编码为5return 5。如果我在可达性代码上方声明一个变量json\u level\u number,它在某种程度上解决了问题,但是它第一次返回了“0”。API每次返回2 代码如下: func fetchNumberOfSections () -> Int {

我试图从JSON对象返回一个结果,但无法返回。我是斯威夫特的新手,请告诉我怎么做。在下面的代码中,我想在函数
fetchNumberOfSections()
的返回中返回
json\u level\u number
,其中我现在硬编码为5
return 5
。如果我在可达性代码上方声明一个变量
json\u level\u number
,它在某种程度上解决了问题,但是它第一次返回了“0”。API每次返回2

代码如下:

func fetchNumberOfSections () -> Int {
    if Reachability.isConnectedToNetwork() == true {
        // Below code to fetch number of sections 
        var urlAsString = "http://themostplayed.com/rest/fetch_challenge_sections.php"
        urlAsString = urlAsString+"?apiKey="+apiKey
        print (urlAsString)
        let url = NSURL(string: urlAsString)!
        let urlSession = NSURLSession.sharedSession()
        let jsonQuery = urlSession.dataTaskWithURL(url, completionHandler: { data, response, error -> Void in
            if (error != nil) {
                print(error!.localizedDescription)
            }
            do {
                let jsonResult = (try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)) as! NSDictionary
                let json_level_number: String! = jsonResult["level_number"] as! String
                //
                dispatch_async(dispatch_get_main_queue(), {
                    //                self.dateLabel.text = jsonDate
                    //                self.timeLabel.text = jsonTime
                    print(json_level_number)  
               //     self.activityIndicatorStop()
                })
            }
            catch let errorJSON {
                print (errorJSON)
                // alert box code below
                let alert = UIAlertController(title: "JSON Error!", message:"Error processing JSON.", preferredStyle: .Alert)
                let action = UIAlertAction(title: "OK", style: .Default) { _ in
                    // Put here any code that you would like to execute when
                    self.dismissViewControllerAnimated(true, completion: {})
                }
                alert.addAction(action)
                self.presentViewController(alert, animated: true, completion: nil)
                // alert box code end
            }
        })
        jsonQuery.resume()
        // End
    }
    else {
        print("Internet connection FAILED")
        self.performSegueWithIdentifier("nointernet", sender: nil)
    }
    return 5
}

再举一个例子:不,仍然无法解决我的问题。您能否更好地解释一下,在返回
fetchNumberOfSections
函数时,我应该如何传递
json\u level\u number
。我使用
fetchNumberOfSections
作为
UITableView
的节数,无法传递正确的值