将String转换为CLLocationDistance

时间:2015-05-21 02:56:16

标签: swift cllocationdistance

我找不到任何有关如何将String转换为CLLocationDistance的示例。例如:

let distance : String = "2000.0"
let mdf = MKDistanceFormatter()
mdf.units = .Metric
var clDistance = mdf.distanceFromString(distance)

我总是得到clDistance = -1

1 个答案:

答案 0 :(得分:5)

您需要澄清2000是什么。米,公里等。就像那样:

let distance : String = "2000.0m"
let mdf = MKDistanceFormatter()
mdf.units = .Metric
var clDistance = mdf.distanceFromString(distance)

distanceFromString以米为单位返回距离。