简体   繁体   English

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

[英]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.

相关问题 “ countByEnumeratingWithState:objects:count:”错误 - “countByEnumeratingWithState:objects:count:” Error 得到-[NSNull countByEnumeratingWithState:objects:count:] - getting -[NSNull countByEnumeratingWithState:objects:count:] 无法识别的选择器UIDeviceRGBColor countByEnumeratingWithState:objects:count: - Unrecognized selector UIDeviceRGBColor countByEnumeratingWithState:objects:count: [__NSCFNumber countByEnumeratingWithState:objects:count:]:无法识别的选择器已发送到实例 - [__NSCFNumber countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance NSFetchedResultsController错误:“ NSInvalidArgumentException”,原因:“-[NSSortDescriptor countByEnumeratingWithState:objects:count:]: - NSFetchedResultsController Error: 'NSInvalidArgumentException', reason: '-[NSSortDescriptor countByEnumeratingWithState:objects:count:]: iOS 10 Beta SDK - [UIDeviceRGBColor countByEnumeratingWithState:objects:count:]:无法识别的选择器 - iOS 10 Beta SDK - [UIDeviceRGBColor countByEnumeratingWithState:objects:count:]: unrecognized selector NSMutableArray在内部如何工作。 是NSMutableArray内部维护链表? - How NSMutableArray work internally. is NSMutableArray maintains linked list internally? 如何在ARC下的countByEnumeratingWithState中安全地存储对象? - How to safely store objects in extra within countByEnumeratingWithState under ARC? 如何使用类对象对NSMutableArray进行排序? - How do I sort an NSMutableArray with class objects in it? [__NSCFString countByEnumeratingWithState:objects:count:]:无法识别的选择器已发送到实例0x16e09f70&#39; - [__NSCFString countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x16e09f70'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM