我完全不熟悉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