从Firebase读取数据时类型转换错误

时间:2019-10-10 12:50:16

标签: swift firebase firebase-realtime-database

我正在使用mapkit制作应用程序,而我尝试从firebase获取用户位置时会导致这三行错误

let dict = recipeSnap.value as! [String:Double?]

let latitude = dict["latitude"] as? Double?

let longitude = dict["longitude"] as? Double?.

我试图用字符串替换double,但是发生新的错误,然后我尝试用AnyObject替换它,它可以工作,但是数据就像Optional(37.785834)。当我使用AnyObject数据执行其他操作时,它总是会导致错误。

这是我的代码

@objc func updateTimer(){

    print("Update location")
    let location = locman.location!.coordinate
    lat = String(location.latitude)
    long = String(location.longitude)
    print(lat)
    print(long)
    let ref = Database.database().reference()
    ref.child("location/User1/longitude").setValue(long)
    ref.child("location/User1/latitude").setValue(lat)
    ref.child("location").observeSingleEvent(of: .value) { (snapshot) in
          self.usern = snapshot.childrenCount
          for snap in snapshot.children{
              let aSnap = snap as! DataSnapshot  
              let dict = aSnap.value as! [String:Double?]
              let latitude = dict["latitude"] as? Double?
              let longitude = dict["longitude"] as? Double?
              print(dict)
              print(latitude)
              print(longitude)

              let point = MKPointAnnotation()
              point.title = "user"
            //point.coordinate = CLLocationCoordinate2D(latitude: CLLocationDegrees(latitude), longitude: CLLocationDegrees(longitude))
              self.mapView.addAnnotation(point)
        }
    }
}

这是我的错误

  

无法将类型'Swift._NSContiguousString'(0x1032681f8)的值强制转换为'NSNumber'(0x1b7a1abf0)。   2019-10-10 20:24:05 mapkitMap [1468:709956]无法将类型“ Swift._NSContiguousString”(0x1032681f8)的值强制转换为“ NSNumber”(0x1b7a1abf0)。   可选(37.785834)

当我尝试用字符串替换Double时,出现新错误

  

无法将类型'__NSCFNumber'(0x1b7a0e0e0)的值强制转换为'NSString'(0x1b7a1aad8)。   2019-10-10 20:20:32.1 mapkitMap [1465:708355]无法将类型'__NSCFNumber'(0x1b7a0e0e0)的值强制转换为'NSString'(0x1b7a1aad8)。

1 个答案:

答案 0 :(得分:0)

您似乎将b_valif ( (WC()->cart->total <= $minimum) && !empty($woocommerce->cart->applied_coupons) ) { // PRINT ERROR MESSAGE } else { // DO YOUR CODE } 的一些存储为字符串值,

let randomWord = "ABEOHSTD";
let userWordThatFail = "BAASE";
let userWord = "BASE";

// optionnaly, uppercase both words.

// we split into letters to make it easiers to process
let randomLetters = randomWord.split('');
let userLetters = userWord.split('');

let score = 0;

//we parse each letter of the user input
userLetters.forEach((letter) => {
  // the letter exists in the random word.
  let indexOfTheCurrentLetter = randomLetters.indexOf(letter);
  // the letter exists, we remove it and increment the score.
  if(indexOfTheCurrentLetter !== -1) {
    randomLetters.splice(indexOfTheCurrentLetter, 1);
    score++;
  }
});

// the user input contains letters that are not in the random input.
if(score < userLetters.length) {
  console.log('fail');
} else {
  console.log('win : ' + score);
}

,因此您不能简单地从数据库中以数字形式检索它们。您需要将它们作为字符串从数据库中读取,然后解析它们的数值。

类似的东西:

latitude