简体   繁体   English

预期返回“ NSURLSessionDataTask”的函数中缺少返回

[英]Missing return in a function expected to return 'NSURLSessionDataTask'

I am following a tutorial on accessing an api and parsing the result. 我正在遵循有关访问api和解析结果的教程。 I am following the tutorial word for word but I cannot run the program because of 'Missing return in a function expected to return 'NSURLSessionDataTask' 我正在逐字学习本教程,但是由于“缺少预期返回“ NSURLSessionDataTask”的函数中的返回),因此无法运行该程序

so I changed the return statement to "return NSURLSessionDataTask" but then got an error saying "Cannot convert return expression of type 'NSURLSessionDataTask.Type" to return type 'NSURLSessionDataTask' 所以我将return语句更改为“ return NSURLSessionDataTask”,但是收到一条错误消息“无法将类型为'NSURLSessionDataTask.Type的返回表达式转换为返回类型'NSURLSessionDataTask'

How do i figure out the return type? 我如何计算退货类型? do I even need a return? 我什至需要退货吗? because in the tutorial there is not return statement (i tried without return as well). 因为在本教程中没有return语句(我也尝试不使用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()



}

Did you really mean to write your own method called dataTaskWithRequest which looks just like the NSURLSession method of the same name? 您是否真的要编写自己的名为dataTaskWithRequest的方法,该方法看起来就像同名的NSURLSession方法一样? The problem is that you said you're writing a method that returns a NSURLSessionTask object, but you don't return anything. 问题是您说要编写一个返回NSURLSessionTask对象的方法,但不返回任何内容。

I'd think you meant something like the following, renaming your own method to something else, and specifying that it's not returning anything itself, because it's not: 我认为您的意思是以下内容,将您自己的方法重命名为其他方法,并指定它本身不返回任何东西,因为它不是:

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()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM