UIView as dictionary key?

mrueg picture mrueg · Jun 7, 2010 · Viewed 7.9k times · Source

I want to have a NSDictionary that maps from UIViews to something else.

However, since UIViews do not implement the NSCopying protocol, I can't use them directly as dictionary keys.

Answer

luvieere picture luvieere · Jun 7, 2010

You can use an NSValue holding the pointer to the UIView and use this as key. NSValues are copyable. but, if the view is destroyed, the NSValue will hold a junk pointer.