我有一个继承自NSObject的类.它使用NSMutableArray来保存子对象,例如使用NSMutableArray*项的People来保存Person对象.如何在项目上实现NSFastEnumerator?
我尝试过以下但是无效:
@interface People : NSObject <NSFastEnumeration>
{
NSMutableArray *items;
}
Run Code Online (Sandbox Code Playgroud)
@implementation ...
- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len
{
if(state->state == 0)
{
state->mutationsPtr = (unsigned long *)self;
state->itemsPtr = items;
state->state = [items count];
return count;
}
else
return 0;
}
Run Code Online (Sandbox Code Playgroud)
ugh*_*fhw 20
您没有正确使用NSFastEnumerationState结构.请参阅NSFastEnumeration Protocol Reference并查看常量部分以查看每个字段的说明.在你的情况下,你应该保持state->mutationsPtr
为零.state->itemsPtr
应该设置为对象的C数组,而不是NSArray或NSMutableArray.您还需要将相同的对象放入作为stackbuf传递的数组中.
但是,由于您使用NSMutableArray来包含要枚举的对象,因此您只需将调用转发给该对象:
- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len {
return [items countByEnumeratingWithState:state objects:stackbuf count:len];
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2337 次 |
最近记录: |