嗨,大家好我试图让这个小游乐场运行时遇到问题,我在Playground中嘲笑这个工作正常但是我在尝试让它在自己的swift
文件中运行时遇到了问题
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
let url = URL(string: "http://reddit.com/r/globaloffensive/new")!
let task = session.dataTask(with: url, completionHandler: {
(data, response, error) in
if error != nil {
print(error!.localizedDescription)
} else {
do {
if let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String: Any] {
dump(json)
}
} catch {
print("Error")
}
}
})
task.resume()
我主要是想弄清楚如何进行API调用,但是当我在自己的文件中运行它并运行
时 swift run test.swift
我
test.swift:1:14: error: use of unresolved identifier 'URLSessionConfiguration'
let config = URLSessionConfiguration.default
^~~~~~~~~~~~~~~~~~~~~~~
test.swift:2:15: error: use of unresolved identifier 'URLSession'
let session = URLSession(configuration: config)
^~~~~~~~~~
test.swift:3:11: error: use of unresolved identifier 'URL'
let url = URL(string: "http://reddit.com/r/globaloffensive/new")!
^~~
任何信息都会非常感谢。
答案 0 :(得分:0)
您需要导入Foundation框架:
import Foundation