I want to insert a object at a given index. and If Is there no object at the index, I want to know that.
Is NSDictionary good choice?
Is there another good solution?
// Initailization
NSMutableDictionary *items = [NSMutaleDictionary dictionary];
...
// insert an object.
[items setObject:item forKey:[NSNumber numberWithInt:3]];
...
// I want to know that there is an object at a given index.
item = [items objectForKey:[NSNumber numberWithInt:4]];
if (item)
// exist
else
// non exist
Of course there is the
NSArrayclass, for an indexed version ofNSDictionary, kind of. However, the indexes in anNSArrayshould be subsequent, so the index begins at 0 and then increments with every object.So when you want to use a random index, you should go with
NSDictionaryand you’re good. The code you provided is absolutely valid and works correctly.