如何设置 URLSession Swift 3 [英] How to set URLSession Swift 3

查看:97
本文介绍了如何设置 URLSession Swift 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,Swift 3 有问题.我有以下代码:

Hey having trouble with Swift 3. I have the following code:

public var urlSession : URLSession?
self.urlSession = URLSession(configuration: URLSessionConfiguration.default,
            delegate: self,
            delegateQueue: OperationQueue.main)

我收到以下错误消息:URLSession 生成 (),不是预期的上下文结果类型 URLSession?"

I am getting the following error message: "URLSession produces (), not the expected contextual result type URLSession?"

我在这里做错了什么?

推荐答案

请问你的 URLSession 函数?

My I ask your URLSession functions?

也许你的代码中的问题是这样的:

Maybe problem in your code is like:

func URLSession(session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
    self.data.append(data as Data)  
}

和:

func URLSession(session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
    if error != nil {
        print("Failed to download data")
    }else {
        print("Data downloaded")
        self.parseJSON()
    }
}

所以代替:

func URLSession(session:

这样做:

func urlSession(_ session:

最终会像:

func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
    self.data.append(data as Data)  
}

func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
    if error != nil {
        print("Failed to download data")
    }else {
        print("Data downloaded")
        self.parseJSON()
    }
}

如果它不能解决您的问题,请使用更多代码更新您的问题.✌️

If its not a solution for your problem please update your question with more code. ✌️

这篇关于如何设置 URLSession Swift 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆