【问题标题】:Do action on JSON complete对 JSON 执行操作完成
【发布时间】:2015-05-06 15:46:44
【问题描述】:

我编写了一个函数,它发出一个 JSON 请求并将结果放入三个数组中。

我想在这个 JSON 请求完全完成之后显示一个视图控制器。

我尝试将操作放在 for-in 循环下方,但这不起作用。

这是我的代码:

func getJSON() {
        var vc = self.storyboard?.instantiateViewControllerWithIdentifier("myVCId") as ViewController
        let urlAsString = "http://localhost:8888/domainchecker/check.php?domain=/" + tfDomain.text;
        println(urlAsString);
        let url = NSURL(string: urlAsString)!
        let urlSession = NSURLSession.sharedSession()
        
        let jsonQuery = urlSession.dataTaskWithURL(url, completionHandler: { data, response, error -> Void in
            if (error != nil) {
                println(error.localizedDescription)
            }
            var err: NSError?
            
            var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSDictionary
            if (err != nil) {
                println("JSON Error \(err!.localizedDescription)")
            }
            let arrayFromJson: [[String:String]] = jsonResult["ITEMS"] as [[String:String]]
            dispatch_async(dispatch_get_main_queue(), {
                for item in arrayFromJson {
                    vc.tableData.append(item["DOMAIN"]!);
                    vc.tablePrice.append(item["PRICE"]!);
                    vc.tableAvailability.append(item["AVAILABLE"]!);
                }
                self.presentViewController(vc, animated: true, completion: nil)
            })
            
        })
        jsonQuery.resume()
    }

这是代码完成后我想做的操作:

self.presentViewController(vc, animated: true, completion: nil)

【问题讨论】:

    标签: json swift action for-in-loop


    【解决方案1】:
    if (arrayFromJson.count > 0) {
                        self.presentViewController(vc, animated: true, completion: nil)
                    }
    

    就是这样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-03
      • 2019-05-17
      • 2015-05-12
      • 1970-01-01
      • 1970-01-01
      • 2019-04-27
      • 2011-09-07
      • 1970-01-01
      相关资源
      最近更新 更多