【发布时间】:2019-08-24 17:49:12
【问题描述】:
我尝试在单元格中显示帖子创建者与当前用户之间的距离。
我现在的错误是
无法转换“字符串”类型的值?到预期的参数类型'CLLocationDegrees'(又名'Double')
在以下代码中:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let lastLocation = locations.last {
let geoCoder = CLGeocoder()
let myLocation = CLLocation(latitude: lastLocation.coordinate.latitude, longitude: lastLocation.coordinate.longitude)
/*Here the error*/ let myBuddysLocation = CLLocation(latitude: job!.distance, longitude: job!.distance)
let distance = myLocation.distance(from: myBuddysLocation) / 1000
print(String(format: "The distance to my buddy is %.01fkm", distance))
}
}
这是我的 JobClass:
class Job {
var distance: String?
let ref: DatabaseReference!
init(distance: String? = nil) {
self.distance = distance
ref = Database.database().reference().child("jobs").childByAutoId()
}
init(snapshot: DataSnapshot){
ref = snapshot.ref
if let value = snapshot.value as? [String : Any] {
distance = value["distance"] as? String
}
}
func save() {
let newPostKey = ref.key
let postDictionary = [
"distance" : self.distance!
] as [String : Any]
self.ref.setValue(postDictionary)
}
}
我希望有人知道如何解决它,如果有帮助,我可以添加更多代码 // 我是编码新手
【问题讨论】:
-
是什么让您认为
String值(距离)可以神奇地变成Double值? -
字符串属性
distance的内容是什么,你到底从DataSnapshot得到了什么?
标签: ios swift uitableview cllocationmanager