Having a list of id idle objects?

How can I implement a class / object that has a link to a bunch of id idle objects?

I need something similar to what UIControl / NSControl has: addTarget:(id)target action:(SEL)action ...; (in my case, I do not need part of UIControlEvents ). I want to use this target / action template (and preferably stay away from the delegate template), but for this I need to make sure that goals added to my object are not saved or loops can be saved.

I think that implementing my own array using malloc / free to make sure the goals are not saved will be one of the solutions, but all these troubles smell as if some solution had already been implemented. There is?

+1
memory-management collections cocoa-touch cocoa
Aug 13 2018-12-12T00:
source share
2 answers

In OS X, you can create an NSPointerArray that will support weak references to its objects using +[NSPointerArray weakObjectsPointerArray] or (if you want a dictionary) NSMapTable , which allows you to hold one or both keys and values ​​weakly.

None of them are currently available on iOS, but the Core Foundation. On OS X or iOS, you can use CFArray or CFDictionary , passing NULL for callbacks to make a collection without managing the memory of the objects it contains. Be warned that if you try to use them as if they were their Cocoa free bridge copies, the collection will not honor your unrestrained / non-copying desire. You will have to use CF functions to interact with them (or hack the wrapper class).

You can also wrap objects in NSValue s using valueWithNonretainedObject: and put them in the Cocoa collection ( NSDictionary / NSarray ). A collection will matter, but the value will not belong to its object.

+4
Aug 13 2018-12-12T00:
source share

You can create a CFArrayRef by specifying callbacks that do not perform recount operations (or just 0 for callbacks if you feel very lazy):

 CFAllocatorRef allocator = 0; const void **values = ...; CFIndex numValues = ...; const CFArrayCallBacks* const callBacks = 0; CFArrayRef arr = CFArrayCreate(allocator, values, numValues, callBacks); 
+1
Aug 13 2018-12-12T00:
source share



All Articles