[英]How to set URLSession Swift 3
Hey having trouble with Swift 3. I have the following code: 嘿,使用Swift 3遇到麻烦了。我有以下代码:
public var urlSession : URLSession?
self.urlSession = URLSession(configuration: URLSessionConfiguration.default,
delegate: self,
delegateQueue: OperationQueue.main)
I am getting the following error message: "URLSession produces (), not the expected contextual result type URLSession?" 我收到以下错误消息: “ URLSession产生(),而不是预期的上下文结果类型URLSession?”
What am I doing wrong here? 我在这里做错了什么?
My I ask your URLSession functions? 我问你的URLSession函数?
Maybe problem in your code is like: 也许您的代码中的问题是这样的:
func URLSession(session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
self.data.append(data as Data)
}
and : 和:
func URLSession(session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
if error != nil {
print("Failed to download data")
}else {
print("Data downloaded")
self.parseJSON()
}
}
so instead of: 所以代替:
func URLSession(session:
do this: 做这个:
func urlSession(_ session:
final will be 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()
}
}
If its not a solution for your problem please update your question with more code. 如果它不能解决您的问题,请使用更多代码更新您的问题。 ✌️ ✌️
Swift 3.0 斯威夫特3.0
let urlString= "URL String"
let myUrl = URL(string: urlString);
let request = NSMutableURLRequest(url:myUrl!);
request.httpMethod = "GET";
let task = URLSession.shared().dataTask(with: request as URLRequest) {
data, response, error in
if error != nil {
print(error!.localizedDescription)
DispatchQueue.main.sync(execute: {
AWLoader.hide()
})
return
}
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSArray
if let parseJSON = json{
print(parseJSON)
} else {
AWLoader.hide()
}
catch{
AWLoader.hide()
print(error)
}
}
task.resume()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.