在NSMutableArray中观察计数 [英] Observing count in NSMutableArray

查看:126
本文介绍了在NSMutableArray中观察计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在计数时得到通知,即。 NSArray中的项目数量发生了变化..
当然我不需要这个,如果我控制添加和删除数组中的对象。但我不是,它在业务流程模型方面无法预测,并且取决于外部因素。
是否有一些简单优雅的解决方案?

I'd like to be notified, when the count, ie. number of items in an NSArray changes.. Of course I wouldn't need this, if I was in control of addition and removal of objects into the array. But I am not, it happens unpredictably with regards to Business Process Model and depends on external factors. Is there some simple elegant solution?

编辑:我当然正在将此更正为NSMutableArray ..

I am correcting this to NSMutableArray of course..

推荐答案

您需要使用 KVC 。但是如何去做呢?毕竟,NSMutableArray不符合其变异方法或内容更改的键值编码。答案是代理-as子类NS [Mutable]数组太麻烦了。

You’ll need to use KVC. But how to go about doing it? After all, NSMutableArray is not Key-Value-Coding compliant for its mutation methods or contents changes. The answer is proxying –as subclassing NS[Mutable]Array is far too much of a hassle.

NSProxy是一个很棒的小类,可以用来拦截发送的消息你的数组就好像你是一个NSMutableArray,然后将它们转发给一些内部实例。不幸的是,它也不符合KVC,因为KVC的胆量存在于NSObject中。那么我们必须使用它。示例界面可能如下所示:

NSProxy is a great little class that you can use to intercept the messages sent to your array as though you were an NSMutableArray, then forward them on to some internal instance. Unfortunately, it is also not KVC compliant, as the guts of KVC live in NSObject. We’ll have to use that, then. A sample interface might look something like this:

@interface CFIKVCMutableArrayProxy : NSObject  {
    NSMutableArray *_innerArray;
}

- (NSUInteger)count;

- (void)insertObject:(id)anObject atIndex:(NSUInteger)index;
- (void)removeObjectAtIndex:(NSUInteger)index;
- (void)addObject:(id)anObject;
- (void)removeLastObject;
- (void)insertObjects:(NSArray *)objects atIndexes:(NSIndexSet *)indexes;
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;

//…

@end

正如您所看到的,我们正在模拟 NSMutableArray 的接口,这是必要的,因为我们的代理应该实现所有内容,就好像它是 NSMutableArray 。这也使得实现尽可能简单,因为我们可以将选择器转发到内部 NSMutableArray 指针。为了简洁起见,我将只实现两种方法来向您展示大致轮廓:

As you can see, we’re simulating an interface for NSMutableArray, which is necessary, as our proxy should implement everything as though it were an NSMutableArray. This also makes the implementation as simple as possible, as we can just forward the selectors on to our inner NSMutableArray pointer. For the sake of brevity, I’ll only implement two methods to show you what a general outline looks like:

@implementation CFIKVCMutableArrayProxy

//…

- (NSUInteger)count {
    return _innerArray.count;
}

- (void)addObject:(id)anObject {
    [self willChangeValueForKey:@"count"];
    [_innerArray addObject:anObject];
    [self didChangeValueForKey:@"count"];
}

- (void)removeLastObject {
    [self willChangeValueForKey:@"count"];
    [_innerArray removeLastObject];
    [self didChangeValueForKey:@"count"];
}

@end

如果你没有机会像这样包装一个数组,然后尝试重新思考你的代码。如果外部依赖项迫使您进入此类角落,请尝试将其删除。解决自己的工具总是一件坏事。

If you have no opportunities to wrap an array like this, then try to re-think your code. If an external dependency is forcing you into this kind of corner, try to remove it. It’s always a bad thing to work around your own tools.

这篇关于在NSMutableArray中观察计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆