首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义NSFastEnumeration方法?

自定义NSFastEnumeration方法?
EN

Stack Overflow用户
提问于 2013-09-16 19:38:27
回答 1查看 138关注 0票数 0

我有一个容器类,它将数据存储在字典中

我想枚举对象,而不是键。

现在我有这样的代码

代码语言:javascript
复制
-(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;
}
EN

回答 1

Stack Overflow用户

发布于 2013-09-16 19:47:58

为什么不像这样使用-allValues属性:

代码语言:javascript
复制
-(NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(__unsafe_unretained id [])buffer count:(NSUInteger)len
{
    return [self.container.allValues countByEnumeratingWithState:state objects:buffer count:len];
}

此外,NSDictionary是一个无序容器,因此您的-objectAtIndex:方法没有任何意义。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18827025

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档