运行 2 个任务合二为一 NSURLSession.sharedSession

running 2 tasks on one NSURLSession.sharedSession

我正在尝试执行 2 个 HTTP 请求 在第一个请求中,我将获得一个将在第二个请求中使用的 var。 我的问题是,当我使用下面的代码执行此操作时,它只是 运行 第二个请求而没有 运行 宁第一个。

    let url = NSURL(string:"https://loginurl")
    let cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData
    let request = NSMutableURLRequest(URL: url!, cachePolicy: cachePolicy, timeoutInterval: 2.0)
    request.HTTPMethod = "POST"

    let contentType = " application/x-www-form-urlencoded"
    NSURLProtocol.setProperty(contentType, forKey: "Content-Type", inRequest: request)

    var dataString = "user details"
    var requestBodyData = (dataString as NSString).dataUsingEncoding(NSUTF8StringEncoding)
    request.HTTPBody = requestBodyData

    NSURLProtocol.setProperty(requestBodyData!.length, forKey: "Content-Length", inRequest: request)

    var response: NSURLResponse?
    let session = NSURLSession.sharedSession()


    var task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in

 //code for getting the session id to be used in the second request

    })
    task.resume()

    let url2 = NSURL(string:"http://url2?sessionid")
    let request2 = NSMutableURLRequest(URL: url2!, cachePolicy: cachePolicy, timeoutInterval: 2.0)
    request2.HTTPMethod = "GET"
     dataString=""
     requestBodyData = (dataString as NSString).dataUsingEncoding(NSUTF8StringEncoding)
    request2.HTTPBody = requestBodyData
    NSURLProtocol.setProperty(contentType, forKey: "Content-Type", inRequest: request2)

      task = session.dataTaskWithRequest(request2, completionHandler: {data, response, error -> Void in


     })

    task.resume()

谁能解释一下这段代码有什么问题

问题 2:

 let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in

        if let httpRes = response as? NSHTTPURLResponse {
            let html = NSString(data:data!, encoding:NSUTF8StringEncoding)


            if let match = html?.rangeOfString("(?<=sessionid=')[^\';]+", options: .RegularExpressionSearch)  {
                portalid = (html?.substringWithRange(match))!


            }

        }

当我 运行 在模拟器中运行时没有问题,但是当我在 iPhone 上调试它时它崩溃并显示此错误 "Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFString substringWithRange:]: Range {9223372036854775807, 0} out of bounds; string length 52427'" 谢谢

这是发出两个请求的代码示例:

//: Playground - noun: a place where people can play

import UIKit
import XCPlayground

XCPlaygroundPage.currentPage.needsIndefiniteExecution = true

let session = NSURLSession.sharedSession()

session.dataTaskWithURL(NSURL(string: "http://www.google.com")!, completionHandler: { (data :NSData?, response :NSURLResponse?, error :NSError?) -> Void in

        let res = NSString(data: data!, encoding: NSUTF8StringEncoding)
        print(res!)

        // make a new request here
        let innerSession = NSURLSession.sharedSession()

    innerSession.dataTaskWithURL(NSURL(string: "http://www.google.com")!, completionHandler: { (data :NSData?, response :NSURLResponse?, error :NSError?) -> Void in

        let res = NSString(data: data!, encoding: NSUTF8StringEncoding)
        print(res!)


    }).resume()


    }).resume()