How to insert an object at a given index in iphone-sdk
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 NSArray
class, for an indexed version of NSDictionary
, kind of. However, the indexes in an NSArray
should 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 NSDictionary
and you're good. The code you provided is absolutely valid and works correctly.
精彩评论