如何实现countByEnumeratingWithState:objects:count:用于内部使用NSMutableArray的类

时间:2014-07-13 12:47:50

标签: ios objective-c nsmutablearray nsfastenumeration

我想用

 for (TBL_CardView *cardView in cardsInHand)
    {
        // <#statements#>
    }

TBL_CardView是我的自定义类,而cardsInHand只是(TBL_CardViewArray*)

所以我需要为countByEnumeratingWithState:objects:count:课程实施TBL_CardViewArray 这是对的吗?

这是我的TBL_CardViewArray.h

/**
 *    Keep TBL_CardView in array
 */
@interface TBL_CardViewArray : NSObject

- (TBL_CardView *)drawCard;
- (void)addCard:(TBL_CardView *)card;
- (NSUInteger)cardsRemaining;
- (NSArray*) cardViewArray;

- (TBL_CardView *)drawRandomCard;

@end 

TBL_CardViewArray.m的一些重要部分

@implementation TBL_CardViewArray
{
    NSMutableArray *_cards;
}

所以我只是使用TBL_CardViewArray作为NSMutableArray的包装来存储我的TBL_CardView类。

问题
如何实现countByEnumeratingWithState:objects:count:for my TBL_CardViewArray class。

我确实谷歌了,但没有找到一些我可以轻松重用的例子 我的假设是因为我已经在使用NSMutableArray存储它并不是那么复杂,但我无法弄清楚如何?

1 个答案:

答案 0 :(得分:7)

非常简单,将其转发给基础NSMutableArray

- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id __unsafe_unretained [])stackbuf count:(NSUInteger)len {
    return [_cards countByEnumeratingWithState:state objects:stackbuf count:len];
}

在枚举数组时,你必须避免变异。