我尝试显示单元格内帖子创建者与当前用户之间的距离。
我的错误是
无法转换“字符串”类型的值?预期的参数类型“ 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)
}
}
我希望有人知道如何解决它,如果您愿意的话,我可以添加更多代码//我是编码新手
答案 0 :(得分:0)
这很正常,因为距离应为Double值。将其更改为:var distance: Double?
应该更正它。
或将值解析为Double let myBuddysLocation = CLLocation(latitude: Double(distance)!, longitude: Double(distance)!)
我建议您避免使用!
,因为如果字符串不是数字,则它可能会产生致命错误,您可以使用guard来保护值,或者让它像下面的示例一样:
if let latitude = Double(distance) {
let myBuddysLocation = CLLocation(latitude: latitude, longitude: latitude)
}
答案 1 :(得分:0)
实际上,在您的函数中,唯一的问题是您需要传递字符串值来代替double,而您需要像这样进行更改。
let myBuddysLocation = CLLocation(latitude: Double.init(job!.distance) ?? 0.0, longitude: Double.init(job!.distance) ?? 0.0)
您需要像这样更改功能
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)
let myBuddysLocation = CLLocation(latitude: Double.init(job!.distance) ?? 0.0, longitude: Double.init(job!.distance) ?? 0.0)
let distance = myLocation.distance(from: myBuddysLocation) / 1000
print(String(format: "The distance to my buddy is %.01fkm", distance))
}
}
答案 2 :(得分:0)
错误是不言自明的:您试图设置String?
而不是CLLocationDegrees
,但是甚至可以使用Double
类型。
有几种解决方法:
1)您可以按照以下步骤更改类定义:
class Job {
// Change distance to Double and set a default value instead of optional
var distance: Double = 0.0
let ref: DatabaseReference!
// Change the init
init(distance: Double = 0.0) {
self.distance = distance
ref = Database.database().reference().child("jobs").childByAutoId()
}
//You have to change all your distance to Double
init(snapshot: DataSnapshot){
ref = snapshot.ref
if let value = snapshot.value as? [String : Any] {
// So change this cast
distance = value["distance"] as Double
}
}
}
2)您可以尝试从 String?转换为 Double ,但是我不建议这样做。
无论如何,这是您可以执行的操作:
if let dist = Double(job!.distance){
let myBuddysLocation = CLLocation(latitude: dist, longitude: dist) -> Error Appears in this Line
let distance = myLocation.distance(from: myBuddysLocation) / 1000
print(String(format: "The distance to my buddy is %.01fkm", distance))
}