How to create an NSDictionary in Objective-C?

vky picture vky · Jan 26, 2017 · Viewed 41.8k times · Source

I want to create an NSDictionary like this type:

"zones": 
{
    {
        "zoneId": "1",
        "locations": 
        {
            {
                "locId": "1",
                "locZoneId": "1",
                "locLatitude": "33.68506785633641",
                "locLongitude": "72.97488212585449"
            },
            {
                "locId": “2”,
                "locZoneId": "1",
                "locLatitude": "33.68506785633641",
                "locLongitude": "72.97488212585449"
            },
            {
                "locId": “3”,
                "locZoneId": "1",
                "locLatitude": "33.68506785633641",
                "locLongitude": "72.97488212585449"
            },
        }
    }
}

But I don't know how to create.

Answer

Marco picture Marco · Jan 26, 2017

You should use a combination of arrays and dictionaries.

Dictionaries are initialized like this:

NSDictionary *dict = @{ key : value, key2 : value2};

Arrays are initialized like this:

NSArray *array = @[Object1, Object2]