ios - 无法在名称处存储类型为 _SwiftValue 的对象。只能存储 NSNumber、NSString、NSDictionary 和 NSArray 类型的对象。

标签 ios swift string firebase firebase-realtime-database

总而言之,我在使用 Firebase 驱动的应用程序时遇到了一些问题。将我的 xCode 升级到版本 8 后出现了这个问题。我对此进行了广泛的研究,并通过一些 Stackoverflow 主题试图解决这个问题,因为这似乎是一个非常常见的错误。我想这可能是我遗漏的非常明显的东西,请原谅我的愚蠢。

这是我收到的错误。

Cannot store object of type _SwiftValue at name. Can only store objects of type NSNumber, NSString, NSDictionary, and NSArray.'

我相信这是我的代码部分。

    func saveUser(withUID uid: String, username: String, value: [String:   String?], completion: @escaping (Error?) -> Void) {
         let values = ["/users/\(uid)": value, "/usernames/\(username)": uid] as [String: Any]
         root.updateChildValues(values) { (error, _) in
            completion(error)
        }
    }

    func updateUser(_ newValue: [String: String?]) {
        currentUser.updateChildValues(newValue)
    }

    // MARK: - Posts

    func createPost(_ _post: [String: String]) {

        let userUID = Auth.shared.currentUserUID
        let key = posts.childByAutoId().key

        var post = _post
        post["userUID"] = userUID

        // get current user's username
        currentUser.child("username").observeSingleEvent(of: .value, with: {     snapshot in
            if let username = snapshot.value as? String {
                post["username"] = username
                let values = [
                    "/posts/\(key)": post,
                    "/user-posts/\(userUID)/\(key)": post
                ]
                self.root.updateChildValues(values)
            }
        })

    }

最佳答案

我的代码中也出现了同样的错误,只是弄明白了。确保 values 的类型是 [String : String] 而不是 [String : String?]

关于ios - 无法在名称处存储类型为 _SwiftValue 的对象。只能存储 NSNumber、NSString、NSDictionary 和 NSArray 类型的对象。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40646900/

相关文章:

objective-c - 有什么巧妙的解决方案可以将资源包含在静态库中?

ios - 如何在 swift 中使用 block /闭包

c# - 在 C# 中的 MessageBox 中显示当前日期时间

python - 使用价格通用解决方案从字符串中删除货币符号和文字

ios - 从 JSON 数据 block 中访问 URL 和数组

cocoa-touch - 在 UITableViewCell 中使用渐变背景

ios - 在 Swift 中重复播放声音文件?

ios - Swift:从 SQLite.swift 访问 NULL 值时应用程序崩溃

swift - 如何将 map 缩放设置设置为 20 - Skobbler map

java - 在java中将字符串写入rtf不会保留结构