[英]How to implement countByEnumeratingWithState:objects:count: for class that internally use NSMutableArray
I wanted to use 我想用
for (TBL_CardView *cardView in cardsInHand)
{
// <#statements#>
}
TBL_CardView is my custom class, and cardsInHand
is just (TBL_CardViewArray*)
TBL_CardView是我的自定义类,而cardsInHand
只是(TBL_CardViewArray*)
So I need to implement countByEnumeratingWithState:objects:count:
for my TBL_CardViewArray
class. 所以我需要实现countByEnumeratingWithState:objects:count:
用于我的TBL_CardViewArray
类。
Is this correct ? 它是否正确 ?
This is my TBL_CardViewArray.h 这是我的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
Some important part from TBL_CardViewArray.m 来自TBL_CardViewArray.m的一些重要部分
@implementation TBL_CardViewArray
{
NSMutableArray *_cards;
}
So I am just using TBL_CardViewArray
as s wrapper around NSMutableArray for storing my TBL_CardView
class. 所以我只是使用TBL_CardViewArray
作为NSMutableArray的包装来存储我的TBL_CardView
类。
Question 题
How to implement countByEnumeratingWithState:objects:count: for my TBL_CardViewArray
class. 如何实现countByEnumeratingWithState:objects:count:用于我的TBL_CardViewArray
类。
I did google it, but not found some example that I could reuse easy. 我确实谷歌它,但没有找到一些我可以轻松重用的例子。
My assumption is that because I am already using NSMutableArray for storing that it is not so complicated, but I can not figure it how ? 我的假设是因为我已经在使用NSMutableArray存储它并不是那么复杂,但我无法弄清楚如何?
Quite simply, forward it to the underlaying NSMutableArray
: 很简单,将它转发到底层的NSMutableArray
:
- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id __unsafe_unretained [])stackbuf count:(NSUInteger)len {
return [_cards countByEnumeratingWithState:state objects:stackbuf count:len];
}
You have to avoid mutating the array while it's being enumerated. 你必须避免在枚举时改变数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.