我正在尝试执行以下脚本:
import Foundation
class TestURLSession{
var session: NSURLSession!
func run(){
session = NSURLSession.sharedSession()
let url = NSURL(string: "http://www.veenex.de/tmp/json")
let request = NSMutableURLRequest(URL: url!)
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.HTTPMethod = "GET"
let getDataTask = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in
// HTTP Response contains an error
if let httpResponse = response as? NSHTTPURLResponse {
if httpResponse.statusCode != 200 {
print("response was not 200: \(response)")
return
}
}
// Error submitting Request
if error != nil {
print("error submitting request: \(error)")
return
}
// print data
if data != nil{
do {
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NSArray
for entry in json {
print(entry)
}
} catch {
print("Error printing data")
}
}
});
getDataTask.resume()
}
}
let testURLSession = TestURLSession()
testURLSession.run()
但我收到错误消息:“错误运行代码:未知错误代码 132。”。 在 Xcode Playground 中执行代码即可。
最佳答案
尚未编写 NSURLSession
的纯 Swift 实现。查看NSURLSession.swift
在 Apple 的 GitHub 存储库中为 Foundation 提交文件。
每个方法都是 NSUnimplemented()
,这意味着它还没有被实现。在完成本类(class)之前,它将无法在 Linux 和 IBM 的 Swift Sandbox 上使用。
关于linux - IBM Swift 沙盒 : Running NSURLSession: Error running code: unknown error code 132,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34926159/