KVO and NSMutableArray

穿精又带淫゛_ 提交于 2019-12-09 02:50:58

问题


How can I set KVO (key-value-observing) with an NSMutableArray?

I want to be notified when a change appears in the array. I never used KVO before with a collection like an array.


回答1:


I think you'll be interested in the answers to this question.

The key is that you can't observe any properties on the array directly—the array is just storage—but you can observe the to-many relationship that's backed by that array (here I'm assuming your array is a property on an object somewhere).

If you don't want to use those special accessors all over the place, your code that owns the array can call

-willChange:valuesAtIndexes:forKey: and

-didChange:valuesAtIndexes:forKey:

as described in this answer.

And if you're on a Mac and not iOS, you should consider NSArrayController.




回答2:


You can use KVOMutableArray. It is a subclass of NSMutableArray, and it supports KVO.

disclaimer: I am the author :)



来源:https://stackoverflow.com/questions/6914248/kvo-and-nsmutablearray

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!