问题
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