Ryan Cocuzzo
Ryan Cocuzzo

Reputation: 3219

Firebase Swift Could not cast value of type '__NSArrayM' (0x10591cc30) to 'NSDictionary'

I am using Swift with Firebase as my backend. For some reason, I am getting this error in one of my snapshots:

Could not cast value of type '__NSArrayM' (0x10591cc30) to 'NSDictionary'

I get this when I use this code:

let snapVal = snapshot.value as! [String: AnyObject]

Why is this happening to just this data snapshot, when it looks like all the other data snapshots?

The data looks like this:

Snap (PERSON) {
    1 =     {
        DESCRIPTION = "Brief description here..";
        "DOB" = "10/15/92, 8:29 PM";
        "STATUS" = 1;
        SONG = "A song";
        "START_DATE" = "10/05/16, 7:59 PM";
    };
}

The snapVal looks like this (when I do not specify it as [String: AnyObject]:

(
    "<null>",
        DESCRIPTION = "Brief description here..";
        "DOB" = "10/15/92, 8:29 PM";
        "STATUS" = 1;
        SONG = "A song";
        "START_DATE" = "10/05/16, 7:59 PM";
    }
)

Why is this the case?

Upvotes: 2

Views: 1091

Answers (2)

Z-Dog
Z-Dog

Reputation: 181

I had the same problem and I fixed it by changing the child value from 0, 1, 2 etc to string values such as p0, p1 which solved my problem. Firebase doesn't actually save a title value for that child when you only use Int's. If that makes sense.

So change the 1 to something like "value1" or "v1" and use swift to reorganise the array with array.sort { $1.status > $0.status }

Snap (PERSON) {
    1 =     {
        DESCRIPTION = "Brief description here..";
        "DOB" = "10/15/92, 8:29 PM";
        "STATUS" = 1;
        SONG = "A song";
        "START_DATE" = "10/05/16, 7:59 PM";
    };
}

to:

Snap (PERSON) {
    "value1" =     {
        DESCRIPTION = "Brief description here..";
        "DOB" = "10/15/92, 8:29 PM";
        "STATUS" = 1;
        SONG = "A song";
        "START_DATE" = "10/05/16, 7:59 PM";
    };
}

Upvotes: 0

Dravidian
Dravidian

Reputation: 9945

  • Make sure that the data that you are retrieving is of type NSDictionary

Then Try changing :-

 let snapVal = snapshot.value as! [String: AnyObject]

To :-

 if let snapVal = snapshot.value as? [String: AnyObject]{

        print(snapVal)

      }

Upvotes: 0

Related Questions