我编写了一个应该从页面返回特定值的函数。我用JSON完成了这个,所以我可以从我的网站获得一定的价值。这是功能:
func getTimerData() {
var currentUsername = PFUser.currentUser()!.username
let url = NSURL(string: "http://dinges.informatica-dinges.nl/timerDataTransmitter.php?user=\(currentUsername!)&unique=85017438957")
let request = NSMutableURLRequest(URL: url!)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
var parseError: NSError?
if let json = NSJSONSerialization.JSONObjectWithData(data!, options: nil, error: &parseError) as? [String: String] {
let rideTime = json["timerValue"]
return rideTime
}
}
}
然而,它给了我错误:Unexpected non-void return value in void function
。我已经尝试了很多,但我似乎找不到解决这个问题的方法。你知道我怎么解决它吗?
答案 0 :(得分:1)
根据需要使用返回数据类型声明您的函数 如:
fun getTimerData() -> String {
var currentUsername = PFUser.currentUser()!.username
let url = NSURL(string: “http://dinges.informatica-dinges.nl/timerDataTransmitter.php?user=\(currentUsername!)&unique=85017438957”)
let request = NSMutableURLRequest(URL: url!)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
var parseError: NSError?
if let json = NSJSONSerialization.JSONObjectWithData(data!, options: nil, error: &parseError) as? [String: String] {
let rideTime = json["timerValue"]
return rideTime
}
}
}