I am reading data from Firebase Data Snapshot which contains an array. I was downcasting this snapshot as [String: AnyObject] into a variable say fetchedDict. Now I want to convert the sensorValues to an swift readable array. I checked dynamicType of sensorValues
print(fetchedDict!["sensorValues"].dynamicType)
It is Optional
I have tried two approaches to convert it in array:
- Downcasting it to NSArray, [Int] but it didnt work.
- Wrote a mirror function(see at the end) which worked on playground but sadly not in iOS App. It gave me Could not cast value of type 'Swift.Array' (0x117a24028) to 'Swift.Int'.
Can someone please guide me to solve this? Thanks!
rootRef!.observeEventType(.Value, withBlock: { (snapshot) in
for child in snapshot.children.allObjects {
let snap = child as! FIRDataSnapshot
let fetchedDict = snap.value as? [String: AnyObject]
})
This is the fetchedDict:
[
"activityDuration": 15;
"sensorValues": (
5,
24,
24,
13,
22,
4,
42,
13,
3,
4
);
"timestamp": 20160713184023;
]
Mirror Function which didnt work in the iOS app. Tried Any as well as AnyObject as argument type. This code works in playground though.
func tupleToArray(sensorValues: Any) -> [Int] {
let mirror = Mirror(reflecting: sensorValues)
var arr = [Int]()
for child in mirror.children{
let stringedValue = (child.value) as! Int
arr.append(stringedValue)
}
return arr
}
child.value? why not justchild as! Int?