ageektrapped
ageektrapped

Reputation: 14562

Is it possible to observe the count of an NSDictionary using 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.

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

Upvotes: 1

Views: 751

Answers (1)

Bored Astronaut
Bored Astronaut

Reputation: 994

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.

Upvotes: 1

Related Questions