Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

swift convert any to Array

erdinc27erdinc27 TurkeyPosts: 396 @ @
I try to get data from url and convert it to Array with swift 3. I parsed the data. It holds an array inside. But when i parse it with swift it gets that array as Any. How i can convert that "Any" object to Array properly.
Here how the data looks
{
    categories =     (
                {
            id = 1;
            name = "First Kategori";
        },
                {
            id = 2;
            name = "Kategori 2";
        },
                {
            id = 3;
            name = "Kategori 3";
        },
                {
            id = 4;
            name = "Kategori 4";
        },
                {
            id = 5;
            name = "Kategori 5";
        }
    );
    error = 0;
    errorText = "No Problem";
}
i want to get "Categories" array from here. Any help would be appreciated.

Replies

  • ryantcbryantcb Posts: 664 @ @ @
    edited December 2016
    It's an Array of Dictionaries but could have been an array of any type (string, UIViews, Int, custom Object... etc) hence why swift declares it as [any]. The swift language needs to be able to work with all those types of arrays and apple didn't write an array Class just to handle your custom object. So they gave us the any type. To tell swift that you know what type of array it is and to then work with the array more appropriate for the content use the swift cast

    yourData as! [Dictionary]

    Then you have an array of Dictionaries and each one has 2 keys Id & name.
    Post edited by ryantcb on
  • Duncan CDuncan C Posts: 9,114 @ @ @ @ @ @ @
    ryantcb wrote: »
    It's an Array of Dictionaries but could have been an array of any type (string, UIViews, Int, custom Object... etc) hence why swift declares it as [any]. The swift language needs to be able to work with all those types of arrays and apple didn't write an array Class just to handle your custom object. So they gave us the any type. To tell swift that you know what type of array it is and to then work with the array more appropriate for the content use the swift cast

    yourData as! [Dictionary]

    Then you have an array of Dictionaries and each one has 2 keys Id & name.

    *also looking at it from my small phone screen you appear to have a syntax error you use ( instead of { after catergories = on line 2

    Actually it looks like it's outer container is a dictionary. The data the OP wants is apparently an array saved in the `categories` key of the outer dictionary.

    Dictionary
    categories: array of dictionaries
    error: Int
    errorText: String

    Regards,
    Duncan C
    WareTo

    widehead.gif
    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
  • erdinc27erdinc27 TurkeyPosts: 396 @ @
    Thanks for the replies guys. As Duncan's said i want to get the data in Categories key. I will try to cast the data as ryantcb suggested. Thank you.
Sign In or Register to comment.