【问题标题】:Custom NSFastEnumeration method?自定义 NSFastEnumeration 方法?
【发布时间】:2013-09-16 11:38:27
【问题描述】:

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

我想枚举对象而不是键。

现在我有这样的代码

-(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;
}

【问题讨论】:

    标签: objective-c containers nsfastenumeration


    【解决方案1】:

    为什么不像这样使用-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: 方法毫无意义。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-03
      • 2011-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-11
      相关资源
      最近更新 更多