我有一个容器类,它将数据存储在字典中
我想枚举对象,而不是键。
现在我有这样的代码
-(NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(__unsafe_unretained id [])buffer count:(NSUInteger)len
{
return [self.container countByEnumeratingWithState:state objects:buffer count:len]; // this isn't working as container is dictionary
}
-(myobject *)objectAtIndex:(int)number // this is the method to retrieve a specific object
{
int i = 0;
for(id key in self.container) {
if (number == i) {
id value = [self.container objectForKey:key];
return value;
}
else i++;
}
return nil;
}发布于 2013-09-16 19:47:58
为什么不像这样使用-allValues属性:
-(NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(__unsafe_unretained id [])buffer count:(NSUInteger)len
{
return [self.container.allValues countByEnumeratingWithState:state objects:buffer count:len];
}此外,NSDictionary是一个无序容器,因此您的-objectAtIndex:方法没有任何意义。
https://stackoverflow.com/questions/18827025
复制相似问题