尝试将代码从Playground移动到其自己的文件时出错

时间:2016-10-15 22:08:39

标签: swift

嗨,大家好我试图让这个小游乐场运行时遇到问题,我在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")!
          ^~~

任何信息都会非常感谢。

1 个答案:

答案 0 :(得分:0)

您需要导入Foundation框架:

import Foundation