在NSMutableArray中观察计数

编程入门 行业动态 更新时间:2024-10-24 20:23:14
本文介绍了在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中观察计数

本文发布于:2023-11-22 08:23:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1616675.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:NSMutableArray

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!