我完全不熟悉Swift或網絡中的開發。我試圖拋出一些簡單的東西,查詢一個URL:使用Swift發送HTTP請求的問題
我想寫一個簡單的程序,我有三個按鈕,每次點擊一個按鈕,我發送數據到data.sparkfun .COM數據流存儲服務,而它將存儲我的變量「test_value」
我需要查詢該網址:閱讀一些簡單的教程後失敗
http://data.sparkfun.com/input/XGyzWVyDdEFKDE42W99r?private_key=KEYVALUE&test_value=1"
這裏是我想與之合作的代碼:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func ColorTwoTapped(_ sender: Any) {
print("Color 2")
}
@IBAction func ColorOneTapped(_ sender: Any) {
print("Color 1")
let scriptUrl = "http://data.sparkfun.com/input/XGyzWVyDdEFKDE42W99r"
let urlWithParams = scriptUrl + "?private_key=KEYVALUE&test_value=1"
let myUrl = NSURL(string: urlWithParams);
let task = URLSession.sharedSession().dataTaskWithURL(myUrl!) {(data, response, error) in
print(String(data: data!, encoding: NSUTF8StringEncoding))
}
task.resume()
}
@IBAction func ColorThreeTapped(_ sender: Any) {
print("Color 3")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
我得到的錯誤是
Cannot call value of non-function type 'URLSession'.
感謝您的任何意見
試試NSURLSession? – koropok