Is it possible to observe the count of an NSDictionary using KVO?

不羁岁月 提交于 2020-01-05 07:56:05

问题


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.

So, my question: is it possible to observe the count of an NSDictionary using KVO?


回答1:


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 is overkill for this. From the proxy, dirty the appropriate view/label when inserting or removing an entry in the dict.



来源:https://stackoverflow.com/questions/8746269/is-it-possible-to-observe-the-count-of-an-nsdictionary-using-kvo

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