无法将'__NSCFNumber'(0x1b4520df0)类型的值转换为'NSString'

时间:2017-04-28 08:15:45

标签: ios swift xcode firebase firebase-realtime-database

 let uid = FIRAuth.auth()?.currentUser?.uid

            let ref = FIRDatabase.database().reference().child("users").child((uid)!)

            ref.observeSingleEvent(of: .value, with: { snapshot in

                if let dictionary = snapshot.value as? [String: AnyObject] {

                    let htest = dictionary["h"] as! String //error is mainly here signal SIGABRT

                    var inthtest = Int(htest)

                    if (inthtest==0){
                        self.checkPointsk()
                        print("scanning1")

我已经尝试将as String Value更改为int Value但它仍然不起作用

1 个答案:

答案 0 :(得分:3)

错误是不言自明的。您正在尝试将数字转换为字符串。替换以下两行

let htest = dictionary["h"] as! String 
var inthtest = Int(htest)

var inthtest = dictionary["h"] as! Int

避免此类崩溃的最佳方法是使用条件绑定。

if var inthtest = dictionary["h"] as? Int{
    //Do your stuff
}
else if var inthtest = dictionary["h"] as? String{
    if let integerValue = Int(inthtest){
        //Do your stuff
    }
}