Firebase的Swift 4解包字典

时间:2018-10-17 18:16:41

标签: swift dictionary swift4

这是“ print(dict)”的输出...

["2018-10-17 11:19:51": {
firstname = Brooke;
id = 40vI7hApqkfX75SWsqIR6cdt7xV2;
lastname = Alvarez;
message = hshahyzhshbsbvash;
username = poiii;
}]
["2018-10-17 11:20:31": {
firstname = Trevor;
id = 40vI7hApqkfX75SWsqIR6cdt7xV2;
lastname = Bellai;
message = hey;
username = br9n;
}]
["2018-10-17 11:20:44": {
firstname = Amy;
id = 40vI7hApqkfX75SWsqIR6cdt7xV2;
lastname = Ikk;
message = hey;
username = nine9;
}]

我的代码...

Database.database().reference().child("recent-msgs").child(uid!).observe(.childAdded) { (snapshot: DataSnapshot) in
    if let dict = snapshot.value as? [String: Any] {
        print(dict)

        // Store data in user.swift model
        let firstnameData = dict[0]["firstname"] as! String
        let idData = dict["id"] as! String
        let lastnameData = dict["lastname"] as! String
        let messageData = dict["message"] as! String
        let usernameData = dict["username"] as! String
        let rankData = dict["rank"] as! String
        let propicrefData = dict["propicref"] as! String
        let convoinfo = RecentConvo(firstnameString: firstnameData, idString: idData, lastnameString: lastnameData, messageString: messageData, usernameString: usernameData, rankString: rankData, propicrefString: propicrefData)
        self.recentconvos.append(convoinfo)
        print(self.recentconvos)
        self.tableView.reloadData()
    }
}

我正在尝试在第一个字典中检索字典,该字典是键的值,该键是与之关联的日期。例如:2018-10-17 11:19:51。但是,我不能使用此确切的字符串来调用它,因为我必须在不知道该字符串的情况下执行此操作。

我尝试过:

let firstnameData = dict[0]["firstname"] as! String

但是它返回一个错误:

  

不能用索引类型为“ Int”的下标“ [String:Any]”

2 个答案:

答案 0 :(得分:0)

出现上述错误是因为您试图访问字典中某个位置(0)的元素。字典不是有序列表,因此没有固定顺序的元素可以访问。

记录的字典实际上看起来并不像字典。假设它是一本字典,并且其键是日期字符串,则可以使用以下代码片段来解析该字典。

class RecentConversation {
    var id: String?
    var firstName: String?
    var lastName: String?
    var message: String?
    var username: String?
    var rank: String?
    var propicref: String?

    init?(dictionary: [String: Any]?) {
        guard let dict = dictionary else {
            // Return nil in case the dictionary passed on is nil
            return nil
        }
        id = dict["id"] as? String
        firstName = dict["firstname"] as? String
        lastName = dict["lastname"] as? String
        message = dict["message"] as? String
        username = dict["username"] as? String
        rank = dict["rank"] as? String
        propicref = dict["propicref"] as? String
    }
}

用法:

let dateStrings = dict.keys.sorted {
    // Sort in chronological order (based on the date string; if you need to sort based on the proper date,
    // convert the date string to Date object and compare the same).
    //
    // Swap the line to $0 > $1 to sort the items reverse chronologically.
    return $0 < $1
}

var conversations: [RecentConversation] = []
for date in dateStrings {
    if let conversation = RecentConversation(dictionary: (dict[date] as? [String: Any])) {
        conversations.append(conversation)
    }
}

答案 1 :(得分:0)

大家都很乐于助人,所以我首先要说声谢谢。我继续使用lionserdar解释的方法。 (.allKeys)

data.frame(rbind(final_df, df))

为我工作,所以我希望这也会对其他人有所帮助!