在预期返回'NSURLSessionDataTask'的函数中缺少返回

时间:2015-11-18 02:16:59

标签: ios iphone swift

我正在学习访问api和解析结果的教程。我正在逐字逐句地学习这个教程,但我无法运行该程序,因为'在预期返回的函数中缺少返回'NSURLSessionDataTask'

所以我将return语句更改为“return NSURLSessionDataTask”,但后来收到错误提示“无法转换类型'NSURLSessionDataTask.Type的返回表达式”返回类型'NSURLSessionDataTask'

我如何找出返回类型?我甚至需要回报吗?因为在教程中没有return语句(我试过没有返回)。

func dataTaskWithRequest(request: NSURLRequest, completionHandler: (NSData?, NSURLResponse?, NSError?) -> Void)
    -> NSURLSessionDataTask {

    let postEndpoint: String = "http://jsonplaceholder.typicode.com/posts/1"
    let urlRequest = NSURLRequest(URL: NSURL(string: postEndpoint)!)


    let config = NSURLSessionConfiguration.defaultSessionConfiguration()
    let session = NSURLSession(configuration: config)


        let task = session.dataTaskWithRequest(urlRequest, completionHandler: {
            (data, response, error) in            

        guard let responseData = data else {
            print("Error: did not recieve data")
            return
        }
        guard error == nil else {
            print("error calling GET on /posts/1")
            print(error)
            return
        }
        // parse the resutl as JSON, since that's what the API provieds
        let post: NSDictionary
        do {
            post  = try NSJSONSerialization.JSONObjectWithData(responseData, options: []) as! NSDictionary
        } catch {
            print("error trying to convert data to JSON")
            return
        }
        // now we have the post, let's just print it to prove we can access it

        print("The post is: " + post.description)

            if let postTitle = post["title"] as? String {
                print("The title is: " + postTitle)
            }




    })

    // and send it
    task.resume()



}

1 个答案:

答案 0 :(得分:1)

你真的打算编写一个名为dataTaskWithRequest的方法,它看起来就像同名的NSURLSession方法吗?问题是你说你正在编写一个返回NSURLSessionTask对象的方法,但你不会返回任何东西。

我认为您的意思如下,将您自己的方法重命名为其他内容,并指明它本身不返回任何内容,因为它不是:

func performRequest() {
    let postEndpoint: String = "http://jsonplaceholder.typicode.com/posts/1"
    let urlRequest = NSURLRequest(URL: NSURL(string: postEndpoint)!)

    let config = NSURLSessionConfiguration.defaultSessionConfiguration()
    let session = NSURLSession(configuration: config)

    let task = session.dataTaskWithRequest(urlRequest, completionHandler: {
        (data, response, error) in

        guard let responseData = data else {
            print("Error: did not recieve data")
            return
        }
        guard error == nil else {
            print("error calling GET on /posts/1")
            print(error)
            return
        }
        // parse the resutl as JSON, since that's what the API provieds
        let post: NSDictionary
        do {
            post  = try NSJSONSerialization.JSONObjectWithData(responseData, options: []) as! NSDictionary
        } catch {
            print("error trying to convert data to JSON")
            return
        }
        // now we have the post, let's just print it to prove we can access it

        print("The post is: " + post.description)

        if let postTitle = post["title"] as? String {
            print("The title is: " + postTitle)
        }
    })

    // and send it
    task.resume()
}