1

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:

  1. Downcasting it to NSArray, [Int] but it didnt work.
  2. 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
 }
6
  • why child.value? why not just child as! Int? Commented Jul 14, 2016 at 11:07
  • does looping through like this work? for i in fetchedDict!["sensorValues"] as! Int Commented Jul 14, 2016 at 11:20
  • @ÖzgürErsil Tried it too...it gives warning Cast from child to unrelated type 'int' always fails. And when i call the function - Execution was interrupted. Commented Jul 14, 2016 at 11:30
  • @jacobbullock no, it doesn't work because it does not conform to protocol seuqenceType. Commented Jul 14, 2016 at 11:33
  • @RyanCyrus how are you fetching that dict? Is that json converting to a dictionary? Commented Jul 14, 2016 at 11:37

1 Answer 1

4

Can you just read in as NSArray and then make a Swift array out of it?

Given

array_node
  0: "index 0"
  1: "index 1"
  2: "index 2"

and to read it

    let myRef = self.myRootRef.childByAppendingPath("array_node")
    myRef.observeSingleEventOfType(.Value, withBlock: { snapshot in

        let a = snapshot.value as! NSArray
        print(a)

        let b = (a as Array).filter {$0 is String}

        print(b)
    })

the output is

(
    "index 0",
    "index 1",
    "index 2"
)
[index 0, index 1, index 2]
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I could have.. but i am iterating through all children. But now i realize i can use conditional checking with what you suggested. Thanks! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.