Something like this should work (with better named variables):
- Cast the
snapshot.value
as a dictionary
- Use the dictionary's
.keys
& .values
properties to retrieve them as LazyMapCollection
types.
- Initialise an array with the collections.
Code:
self.ref?
.child("data")
.child("success")
.child(userID!)
.observeSingleEvent(of: .value, with: { (snapshot) in
if let data = snapshot.value as? [String: Any] {
let keys = Array(data.keys)
let values = Array(data.values)
... // Your code here
}
}
Update - To show example where order is respected:
I would probably save the dictionary to an array, which casts it to type Array<(key: String, value: Any)>
. Then, you can map it to keys or values and keep the order.
... // As above
.observeSingleEvent(of: .value, with: { (snapshot) in
if let data = snapshot.value as? [String: Any] {
let dataArray = Array(data)
let keys = dataArray.map { $0.0 }
let values = dataArray.map { $0.1 }
... // Your code here
}
}