是否可以使用KVO观察NSDictionary的计数? [英] Is it possible to observe the count of an NSDictionary using KVO?
问题描述
我想根据NSDictionary的计数设置UITabItem
的徽章值.我想在没有太多代码的情况下执行此操作. KVO似乎是要走的路,但是我似乎仅凭观察字典的数量就找不到任何东西,这使我怀疑这是不可能的.
I want to be set the badge value of a UITabItem
based on the count of an NSDictionary. I'd like to do this without too much code. KVO seems the way to go, but I can't seem to find anything on simply observing the count of the dictionary, which makes me suspect this is not possible.
所以,我的问题是:是否可以使用KVO观察NSDictionary的计数?
So, my question: is it possible to observe the count of an NSDictionary using KVO?
推荐答案
将字典包装在具有您可以观察到的count属性的另一个对象(代理)中.您可能很花哨(以更多代码为代价)并使用了调用转发,但是您可能只需要包装基本的-setObject:forKey:和-removeObjectForKey:(可能还有-removeAllObjects)方法,并只需设置count属性即可.不管字典的计数值是多少.
Wrap the dictionary in another object (a proxy) that has a count property that you can observe. You could be all fancy (at the cost of more code) and use invocation forwarding, but you probably only need to wrap the basic -setObject:forKey: and -removeObjectForKey: (and maybe -removeAllObjects) methods, and just set the count property with whatever the dict's count value is.
KVO在这方面是过大的.在代理中插入或删除字典中的条目时,从代理中弄脏适当的视图/标签.
KVO is overkill for this. From the proxy, dirty the appropriate view/label when inserting or removing an entry in the dict.
这篇关于是否可以使用KVO观察NSDictionary的计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!