我试图将紧急数据解析为紧急结构,但它从不分析条件并进入其他情况。这是我的代码和结构。我在第一行写了一些内容。
if let emergencyDict = snapshotValue["emergency"] as? [String:[String:Any]]{
for (emerId, emerData) in emergencyDict {
let emer = Emergency.init(emergency: emerData as NSDictionary)
emergency.append(emer)
}
}
else{
let emer = Emergency.init(emerg: "" as AnyObject)
emergency.append(emer)
}
struct Emergency{
var emer_id: String
var emer_name: String
var emer_phoneNo: String
init(emergency: NSDictionary) {
if emergency.object(forKey: "id") != nil {
emer_id = emergency.object(forKey: "id") as! String
}
else{
emer_id = ""
}
}
}
答案 0 :(得分:1)
问题是[Any]
作为类型为[[String:Any]]
的数组,如果删除第一个对象,则会得到类型为if let array = snapshotValue["emergency"] as? [Any],
let emergencyArrar = Array(array.dropFirst()) as? [[String:Any]] {
print(emergencyArray)
for emergency in emergencyArray {
print(emergency)
}
}
的数组。所以试试这样。
SELECT
column1,column2,...
FROM
table
LIMIT offset , count;
答案 1 :(得分:0)
你在这一行写错了:
if let emergencyDict = snapshotValue["emergency"] as? [String:[String:Any]]{
应该是:
if let emergencyDict = snapshotValue["emergency"] as? [[String:Any]]{
答案 2 :(得分:0)
此问题应属于来自firebase数据库的查询。
// you have to get the children in emergency,
// then get the value(dictionary) of each child
ref.child("emergency").observeSingleEvent(of: .value, with: { (snapshot) in
let value = snapshot.value as? NSDictionary
let keys = value?.allKeys // [1, 2, 3 ....]
for key in keys {
ref.child("emergency").child(key)..observeSingleEvent(of: .value, with: { (snapshot) in
let value = snapshot.value as? NSDictionary
// Here is your dictionary
}
}
}) { (error) in
print(error.localizedDescription)
}