对JSON执行操作完成



我编写了一个函数,该函数生成JSON请求并将结果放入三个数组中。

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

我试着把动作放在for的循环下面,但没有成功。

这是我的代码:

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)
if (arrayFromJson.count > 0) {
                    self.presentViewController(vc, animated: true, completion: nil)
                }

就是这样。

最新更新