2016-10-15 22 views
0

嗨,大家好我有問題想要讓這個小操場運行,我在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

您需要導入Foundation框架:

import Foundation 
相關問題