ios - 多个 NSURLSession 加载 Json

标签 ios swift swift2 nsurlsession

我想加载两个 NSURLSession。问题是在第二个 NSURLSession 中,我需要一个由第一个 NSURLSession 给出的 key 。

这就是我所做的,但它似乎不起作用!

NSURLSession.sharedSession().dataTaskWithURL(url!) { (data, response, error) in
     do {

let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers)

// fetching items from json ...

for item in items {


 // I extract an ID from each item

 let myID = item["id"] as! String
  /* Here is the problem I want to call another NSURLSession with 
   a url that contains this myID (String) */



 let url2 = NSURL(string:"http://example.com/something?ID=\(myID)")
 NSURLSession.sharedSession().dataTaskWithURL(url2!) { (data2, response2, error2) in
     do {

   //The problem is that this code is not executed in each for loop !

       } catch let jsonError2 {
            print(jsonError2)
        }  

}.resume()





  }

       } catch let jsonError {
            print(jsonError)
        }  

}.resume()

我该怎么办?

最佳答案

对于那些感兴趣的人,解决方案是保留这段代码并添加一个对数组中的数据进行排序的函数。

关于ios - 多个 NSURLSession 加载 Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38834957/

相关文章:

ios - UITableViewCell上CustomCell的点击识别器无法点击

ios - UITableview cellForRowAtIndexPath 方法没有执行?

ios - 如何创建带有大图像的苹果推送通知横幅

ios - 如何过滤json swift中的内容

swift - 如何在 Swift Realm 中为记录指定特定 ID

iOS - float 单按钮

ios - 创建 iOS 模拟器构建(供 Facebook 审核)

ios - iOS 7.1 中的 UILabel 虚线颜色错误

ios - 如何快速将 Carbon Kit 添加到我的项目中?

ios - 在 RTL 布局中强制 UIImageView 对齐 LTR