3

I want to keep a mutable array of weak references to a group of UIViews. NSMapTable is perfect for this need, except for one detail... NSMapTable wants a key for every value it holds.

Is there something like an NSMapArray? If not, do I need to generate throw-away unique keys for all the values I store? Should I use NSUUID to generate the key names?

EDIT: Please not that I'm talking about truly weak, self-zeroing references. CFArrays and NSValue solutions store references which won't nil themselves out when their referenced object gets deallocated...they'll just end up as junk pointers.

zakdances
  • 22,285
  • 32
  • 102
  • 173

3 Answers3

1

You can try NSHashTable on iOS 6, which is like a NSSet, with support for weak references (and NSPointerArray looks nice too, but the doc says it does not support weak references under ARC).

Edit: Some people seem to think that contrary to what the doc says, NSPointerArray does zero weak references under ARC. The OS X 10.8 Foundation Release Notes say so, even though the class documentation says the opposite (iOS release notes don't say). After all, that's why they added the + weakObjectsPointerArray constructor, and the NSPointerFunctionsWeakMemory option in OS X 10.8 and iOS 6.0... You should try...

Guillaume
  • 4,331
  • 2
  • 28
  • 31
  • That's excellent, I didn't know about NSHashTable. Is there something like that for ordered sets? – zakdances Mar 26 '13 at 11:40
  • As I said in the edit, you should try `NSPointerArray` even though the documentation is confusing whether it does zero weak references or not under ARC on iOS. It looks like it does and the documentation is outdated, but you should definitely try before making assumptions. – Guillaume Mar 26 '13 at 13:48
  • This sounds like exactly what I'm looking for. From the docs: `In a garbage collected environment, if you specify a zeroing weak memory configuration, if an element is collected it is replaced by a NULL value.` That's not confusing. – zakdances Mar 26 '13 at 13:50
  • 1
    Yes, but iOS is not a garbage collected environment. And there is a big box saying `Important: NSPointerArray does not support weak references under Automatic Reference Counting (ARC).` That's where the confusion is! – Guillaume Mar 26 '13 at 14:23
0

You said it is important to store weak references in an array. This is possible using Core Foundation arrays. You will find the answer here: Non-retaining array for delegates.

Community
  • 1
  • 1
Reinhard Männer
  • 14,022
  • 5
  • 54
  • 116
  • This is the best answer so far, however I asked this question to see if there is a better answer than catagories or CFArray hacks. As you might have read in the comments, there are a number of problems with that method (references won't self-nil, for example). – zakdances Mar 26 '13 at 06:58
0

What you say is kind of contradictory: On one hand you said you want them to be zeroing weak references, in which case your array will be filled with nil elements after they are deallocated. On the other hand you said you want it to be like NSMapTable, which may remove elements after the weak references are deallocated. Which is it?

If you want it to be truly zeroing weak references (which will leave nil elements in your array), one good solution is to use a std::vector<id __weak> -- a C++ vector whose elements are zeroing weak references. ARC will handle all the details.

newacct
  • 119,665
  • 29
  • 163
  • 224