【问题标题】:How to implement countByEnumeratingWithState:objects:count: for class that internally use NSMutableArray如何为内部使用 NSMutableArray 的类实现 countByEnumeratingWithState:objects:count:
【发布时间】:2014-07-13 12:47:50
【问题描述】:

我想用

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

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

所以我需要为我的TBL_CardViewArray 类实现countByEnumeratingWithState:objects:count:
这是正确的吗?

这是我的 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;
}

所以我只是在 NSMutableArray 周围使用 TBL_CardViewArrayas 包装器来存储我的 TBL_CardViewclass。

问题
如何为我的TBL_CardViewArray 类实现 countByEnumeratingWithState:objects:count:。

我用谷歌搜索过,但没有找到可以轻松重复使用的示例。
我的假设是,因为我已经在使用 NSMutableArray 进行存储,所以它并不复杂,但我不知道怎么做?

【问题讨论】:

    标签: ios objective-c nsmutablearray nsfastenumeration


    【解决方案1】:

    很简单,转发给底层NSMutableArray

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

    您必须避免在枚举数组时对其进行变异。

    【讨论】:

    • 当我将这个添加到我的 m 文件中时,我得到了Must explicitly describe intended ownership of an object array parameter
    • 所以我把它改成- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(__unsafe_unretained id [])stackbuf count:(NSUInteger)len这有效吗?
    • Apple 文档始终是最新的。
    • 不是,但标题是。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-17
    • 2011-06-22
    • 1970-01-01
    • 2012-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多