value:Optional({
"-LMpUBhCddHrShVWPVAx" = {
mobile = "(555) 564-8583";
name = "Kate Bell";
};
"-LMpUBhD9-sX8p2Y3EuB" = {
mobile = "(408) 555-5270";
name = "Daniel Higgins Jr.";
};
"-LMpUBhD9-sX8p2Y3EuC" = {
mobile = "888-555-5512";
name = "John Appleseed";
};
})
我有这套从Firebase获得的字典数据。但是,如何将其保存到阵列中?我想要的数据是移动数据和名称。
这是我的代码:
func retrieveLists() {
ref = Database.database().reference()
ref.child("users").child("60123456789").child("contact_lists").observeSingleEvent(of: .value, with: { (snapshot) in
let value = snapshot.value as? NSDictionary
print("value:\(value)")
}) { (error) in
print(error.localizedDescription)
}
}
答案 0 :(得分:5)
使用以下代码作为参考:-
if let firebaseDic = snapshot.value as? [String: AnyObject] {
for (_, value) in firebaseDic.enumerated() {
let mobileNumber = value.value["mobile"]!! as! String
}
}
答案 1 :(得分:1)
您可以尝试
struct Item {
let mobile:String
let name:String
}
//
var content = [Item]()
for k in value.allKeys {
if let res = value[k] as? [String:String] , let mobile = res["mobile"] , let name = res["name"] {
content.append(Item(mobile: mobile, name: name))
}
}