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.
Creating NSDictionary Objects Using Dictionary Literals In addition to the provided initializers, such as init(objects:forKeys:) , you can create an NSDictionary object using a dictionary literal. In Objective-C, the compiler generates code that makes an underlying call to the init(objects:forKeys:count:) method.
Creating NSArray Objects Using Array Literals In addition to the provided initializers, such as initWithObjects: , you can create an NSArray object using an array literal. In Objective-C, the compiler generates code that makes an underlying call to the init(objects:count:) method.
An object representing a dynamic collection of key-value pairs, for use instead of a Dictionary variable in cases that require reference semantics.
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]
The following has a strongly typed key
as NSString
and the value
as NSNumber
.
You should always set the types where you can, because by making it strongly typed the compiler will stop you from making common errors and it works better with Swift:
NSDictionary<NSString *, NSNumber *> *numberDictionary;
but in the case above, we need to store an array in the dictionary, so it will be:
NSDictionary<NSString *, id> *dataDictionary;
which allows the value to be of any type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With