Swift - 无法将“__NSCFString"类型的值转换为“NSDictionary" [英] Swift - Could not cast value of type '__NSCFString' to 'NSDictionary'

查看:31
本文介绍了Swift - 无法将“__NSCFString"类型的值转换为“NSDictionary"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误,但我正在尝试从 Dictionary 获取 String.这是我的代码:

I got this error, but I'm trying to get a String from a Dictionary. This is my code:

FIRDatabase.database().reference().child("users").child(uid).observeEventType(.ChildAdded, withBlock: { (snapshot) in

            let dictionary = snapshot.value as! NSDictionary

            if let username = dictionary["name"] as? String {
                cell.name.text = username
            }

            if let userlogin = dictionary["login"] as? String {
                cell.login.text = userlogin
            }

        })

在我的 Firebase 数据库中,"name""login" 都是字符串.我不明白这是什么问题.

In my Firebase Database "name" and "login" are both Strings. I cannot understand what's the problem.

任何帮助将不胜感激!

推荐答案

问题将快照转换为 NSDictionary.由于快照值是一个字符串.试试这个:

Issue regards snapshot cast to NSDictionary. Since snapshot value is a String. Try this:

FIRDatabase.database().reference().child("users").child(uid).observeEventType(.ChildAdded, withBlock: { (snapshot) in

        if let dictionary = snapshot.value as? NSDictionary {

            if let username = dictionary["name"] as? String {
                cell.name.text = username
            }

            if let userlogin = dictionary["login"] as? String {
                cell.login.text = userlogin
            }
        }
    })

这篇关于Swift - 无法将“__NSCFString"类型的值转换为“NSDictionary"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆