ios - 快速将字符串转换为 double 型

标签 ios string swift double swift2

我正在尝试查询我的用户的距离。我可以用这段代码做到这一点

    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 值,但我不明白如何通过阅读其他问题来做到这一点。总的来说,我对编码还很陌生。有人可以告诉我我需要做什么吗?

SO convert string to double

最佳答案

只需使用初始化器将 distanceSearch 转换为 Double:

query?.whereKey("location", nearGeoPoint: geoPoint!, withinKilometers: Double(distanceSearch!)!)

关于ios - 快速将字符串转换为 double 型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33899816/

相关文章:

objective-c - 以编程方式设置 NSIndexPath

Java 字符串统计字母出现次数

PHP:包含为没有输出缓冲的字符串?

ios - FileManager MoveItem 回调 - Swift

ios - CoreBluetooth 和 BluetoothManager,设备不是 BLE

ios - MT2002 : Failed to resolve assembly: System. 网络

iphone - 如何对@distinctUnionOfObjects的结果进行排序?

ios - 无法在文件夹中写入 nsdata

javascript - 获取以分号分隔的字符串中的数字并计算平均值jquery

swift - 找不到接受提供的参数的 '__conversion' 的重载,将多个元素添加到数组