【问题标题】:Cannot convert value of type 'String?' to expected argument type 'CLLocationDegrees' (aka 'Double')无法转换“字符串”类型的值?到预期的参数类型'CLLocationDegrees'(又名'Double')
【发布时间】: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


【解决方案1】:

错误是不言自明的:您尝试设置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))
   }

【讨论】:

  • 什么意思?在我看来,他似乎不是在尝试在地图上创建一个点,而是在尝试检索两个位置之间的距离。
  • 我说的是第二个位置,myBuddysLocation
  • 第二个位置取自一个对象,我们不知道他是如何填充它的,但它有利于计算与另一个位置的距离,当然要知道可能的结果。无论如何,我认为这是题外话,他要求解决编译错误,而不是运行时或逻辑错误。
  • 好吧,也许反对投票有点苛刻,但正如你所说,你的答案只不过是指出一个简单的编译错误,也许对 OP 的问题发表评论就足够了。
  • 我不知道 OP 的问题是什么意思,但如果你能帮助我理解,我很乐意根据你的建议改进这个答案。
【解决方案2】:

实际上在你的函数中唯一的问题是你传递字符串值来代替你需要像这样改变的双精度值。

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))
    }
}

【讨论】:

  • @JoakimDanielson 我没有使用单个坐标值,但我只解决了他的错误。我使用有问题的相同代码
  • 好吧,OP 问题中的错误比这更深,你不这么认为吗?
  • @JoakimDanielson 好吧,我不确定这一点,因为他没有准确给出从数据库中获取的值
  • 那为什么不让 OP 澄清一下呢?
  • @JoakimDanielson 是的,我没想到,谢谢您的建议。
【解决方案3】:

这是正常的,因为距离应该是 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)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-18
    • 2019-12-24
    • 1970-01-01
    • 2018-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多