无法指定'String'类型的值?输入'CLLocationDegrees?' (又名'可选<双>')

时间:2017-10-22 19:28:47

标签: ios swift xcode

嘿伙计们,我在这段代码中遇到了这个错误:

 @IBAction func requestGroomrButton(_ sender: UIButton) {

    if pickUpNotificationOn == false {

        var locationForRider = ["Latitude": self.latitude, "Longitude": self.longitude]


        let fireUser = Auth.auth().currentUser
        locationForRider["Email"] = fireUser!.email <-ERROR HERE

错误在最后一行并说:不能指定'String'类型的值?输入'CLLocationDegrees?' (又名'可选')

这是我的fireUser变量:

if let fireUser = Auth.auth().currentUser {
      FirebaseData.fb.REF_BASE.child(PICKUP_NOTIFICATION).child(fireUser.uid).observe(.value, with: {snapshot in

            if snapshot.hasChild("driverAccepted") {

                FirebaseData.fb.REF_BASE.child(DRIVER_LOCATION).child(fireUser.uid).observe(DataEventType.value, with: {driverSnapshot in

                    if let snapshots = driverSnapshot.children.allObjects as? [DataSnapshot] {

                        for snap in snapshots {
                            if let dLat = snap.value["driverLat"] as? Double {
                                self.driverLatitude = dLat
                            }
                            if let dLong = snap.value["driverLong"] as? Double {
                                self.driverLongitude = dLong
                            }

编辑:我正在使用Swift 4和最新版本的Xcode(Xcode 9)

2 个答案:

答案 0 :(得分:0)

编译器将locationForRider的类型推断为类型[String: CLLocationDegrees]。然后您尝试设置String的值,这是不可能的。

您应该通过将locationForRider的类型声明为[String:Any]来帮助编译器:

var locationForRider: [String: Any] = ["Latitude": self.latitude, "Longitude": self.longitude]```

答案 1 :(得分:0)

错误非常明确:编译器将字典locationForRider的类型推断为[String:CLLocationDegrees]。如果您想拥有异类字典,则必须将类型注释为[String:Any]

var locationForRider : [String:Any] = ["Latitude": self.latitude, "Longitude": self.longitude]