![](/img/trans.png)
[英]How to Fix: Cannot assign value of type 'CLLocationDegrees' (aka 'Double') to type 'Float!' in Swift 3
[英]Cannot assign value of type 'String?' to type 'CLLocationDegrees?' (aka 'Optional<Double>')
嘿伙计们,我在此代码块中收到此错误:
@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
错误在最后一行并说:无法分配“字符串”类型的值? 输入“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)
编译器将locationForRider
的类型推断为[String: CLLocationDegrees]
类型。 然后您尝试设置String
的值,这是不可能的。
您应该通过将locationForRider
的类型声明为[String:Any]
来帮助编译器:
var locationForRider: [String: Any] = ["Latitude": self.latitude, "Longitude": self.longitude]```
错误很明显:编译器将字典locationForRider
的类型推断为[String:CLLocationDegrees]
。 如果你想要一个异构字典,你必须将类型注释为[String:Any]
var locationForRider : [String:Any] = ["Latitude": self.latitude, "Longitude": self.longitude]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.