如何在iphone-sdk中的给定索引处插入对象

时间:2010-01-09 10:36:47

标签: iphone nsdictionary

我想在给定索引处插入一个对象。如果索引上没有对象,我想知道。 NSDictionary是不错的选择? 还有其他好的解决方案吗?

// 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

1 个答案:

答案 0 :(得分:3)

当然,对于NSArray的索引版本,有NSDictionary类。但是,NSArray中的索引应该是后续的,因此索引从0开始,然后随每个对象递增。

因此,当您想使用随机索引时,您应该使用NSDictionary并且您很好。您提供的代码绝对有效且工作正常。

相关问题