简体   繁体   English

无法将“NSTaggedPointerString”类型的值转换为“NSDictionary”。 斯威夫特/火力地堡

[英]Could not cast value of type 'NSTaggedPointerString' to 'NSDictionary'. Swift/Firebase

When listening to a new object in Firebase, I get this error: Could not cast value of type NSTaggedPointerString to NSDictionary . 在Firebase中收听新对象时,我收到此错误:无法将NSTaggedPointerString类型的值NSTaggedPointerStringNSDictionary Here is the code that listens to aa user that has registered. 以下是侦听已注册用户的代码。

FIRAuth.auth()!.addStateDidChangeListener { auth, user in
     guard let user = user else { return }
     self.user = User(authData: user)

     let userref = FIRDatabase.database().reference(withPath: "users").child(self.user.uid)
     userref.observe(.value, with: { snapshot in
          print(snapshot.value!)
          var newItems: [UserItem] = []
          for item in snapshot.children {
               let userDetail = UserItem(snapshot: item as! FIRDataSnapshot) /////////// CRASHES HERE //////////
               newItems.append(userDetail)
          }
     self.userItem = newItems
     })
     print(self.user.uid)
}

And when the error comes up it points to here: 当错误出现时,它指向这里:

    init(snapshot: FIRDataSnapshot) {
        key = snapshot.key
        let snapshotValue = snapshot.value as! [String:Any] ///// ERROR HERE
        name = snapshotValue["name"] as! String
        email = snapshotValue["email"] as! String
        age = snapshotValue["age"] as! String
        ref = snapshot.ref
}

The console is printing what is inside Firebase correctly and the data is there and it exists, but this casting or data read error keeps happening. 控制台正在打印Firebase内部的内容并且数据存在且存在,但此转换或数据读取错误仍在继续。 It has something to do with the conversion but I'm not exactly sure. 它与转换有关,但我不确定。

Say

snapshot.value as! String

In this case, the value is a string, so that is what you must cast to. 在这种情况下,值是一个字符串,因此您必须转换为。

可能是您更新数据库并且旧数据格式不正确,尝试删除数据库,添加新数据并再次运行程序

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将'NSTaggedPointerString'类型的值转换为'NSNumber' - 从PHP到Swift - Could not cast value of type 'NSTaggedPointerString' to 'NSNumber' - from PHP to Swift 无法将类型'NSTaggedPointerString'的值转换为数组 - Could not cast value of type 'NSTaggedPointerString' to an array 无法将'NSTaggedPointerString'类型的值转换为'NSArray' - Could not cast value of type 'NSTaggedPointerString' to 'NSArray' 无法将“NSTaggedPointerString”类型的值转换为“NSNumber” - Could not cast value of type 'NSTaggedPointerString' to 'NSNumber' 无法在 Swift 中将“__NSSingleObjectArrayI”类型的值转换为“NSDictionary” - Could not cast value of type '__NSSingleObjectArrayI' to 'NSDictionary' in Swift Swift:无法将'__NSCFArray'类型的值转换为'NSDictionary' - Swift: Could not cast value of type '__NSCFArray' to 'NSDictionary' 如何解决此问题:“无法将类型'NSTaggedPointerString'(0x105d25e88)的值强制转换为'NSDictionary'(0x105d261a8)。” - How to resolve this: “Could not cast value of type 'NSTaggedPointerString' (0x105d25e88) to 'NSDictionary' (0x105d261a8).” Swift错误:无法将'NSTaggedPointerString'(0x1a1264378)类型的值转换为'NSNumber'(0x1a126f900) - Swift error: Could not cast value of type 'NSTaggedPointerString' (0x1a1264378) to 'NSNumber' (0x1a126f900) Firebase Swift无法将'__NSArrayM'(0x10591cc30)类型的值转换为'NSDictionary' - Firebase Swift Could not cast value of type '__NSArrayM' (0x10591cc30) to 'NSDictionary' 获取错误无法将类型“ NSTaggedPointerString”的值强制转换为“ NSArray” - Get error Could not cast value of type 'NSTaggedPointerString' to 'NSArray'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM