Swift 尝试将代码从游乐场移动到其自己的文件时出错

Swift 尝试将代码从游乐场移动到其自己的文件时出错,swift,Swift,大家好,我在试着让这个小游乐场运行时遇到了问题,我在游乐场模拟了这个,效果很好,但我在试着让它在自己的swift文件中运行时遇到了问题 let config = URLSessionConfiguration.default let session = URLSession(configuration: config) let url = URL(string: "http://reddit.com/r/globaloffensive/new")! let task = session.dat

大家好,我在试着让这个小游乐场运行时遇到了问题,我在游乐场模拟了这个,效果很好,但我在试着让它在自己的
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运行测试。swift
I获取

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")!
          ^~~

任何信息都会非常感谢。

< p>您需要导入基础框架:

import Foundation