我正在尝试查询我的用户的距离。我可以用这段代码做到这一点
PFGeoPoint.geoPointForCurrentLocationInBackground { (geoPoint: PFGeoPoint?, error: NSError?) -> Void in
let query = PFUser.query()
query?.whereKey("location", nearGeoPoint: geoPoint!, withinKilometers: 1.0)
我想做的是,当用户设置 slider 值时,使用该值而不是上面所示的 1.0。我试图用这段代码来做到这一点: 这是 slider 代码:
var distanceSearch: String?
@IBAction func kmSliderValueChanged(sender: UISlider) {
let kmCurrentValue = Int(sender.value)
kmLabelUpdate.text = "\(kmCurrentValue)km"
distanceSearch = "\(kmCurrentValue)"
}
然后这是查询行:
query?.whereKey("location", nearGeoPoint: geoPoint!, withinKilometers: distanceSearch!)
它在查询行上返回错误:
cannot convert value of type String to expected argument type Double
所以我需要将 slider kmCurrentValue 转换为 double 值,但我不明白如何通过阅读其他问题来做到这一点。总的来说,我对编码还很陌生。有人可以告诉我我需要做什么吗?
最佳答案
只需使用初始化器将 distanceSearch
转换为 Double
:
query?.whereKey("location", nearGeoPoint: geoPoint!, withinKilometers: Double(distanceSearch!)!)
关于ios - 快速将字符串转换为 double 型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33899816/