Unsupported array for delegates

In a Cocoa Touch project, I need a specific class to have not only one delegate object, but also many of them.

It seems like I should create an NSArray for these delegates; the problem is that NSArray would save all these delegates, which it should not (should not save its delegates for convention objects).

Should I write my own array class to prevent persistence or are there simpler methods? Thank!

+42
iphone cocoa-touch delegates nsarray
Jan 14 2018-11-14T00:
source share
10 answers

I found this bit of code a while ago (I can’t remember who to bind it to).

This is quite interesting, using the category to allow the creation of a mutable array that does not save / free, supporting it with CFArray with the appropriate callbacks.

 @implementation NSMutableArray (WeakReferences) + (id)mutableArrayUsingWeakReferences { return [self mutableArrayUsingWeakReferencesWithCapacity:0]; } + (id)mutableArrayUsingWeakReferencesWithCapacity:(NSUInteger)capacity { CFArrayCallBacks callbacks = {0, NULL, NULL, CFCopyDescription, CFEqual}; // We create a weak reference array return (id)(CFArrayCreateMutable(0, capacity, &callbacks)); } @end 

EDIT Original article found: http://ofcodeandmen.poltras.com

+48
Jan 14 2018-11-14T00:
source share
β€” -

I present an important limitation of one of the early answers, as well as explanation and improvement.

Jonmf suggested using [NSValue valueWithNonretainedObject:] .

Note that when you do this, your link does not act as __weak , but rather as __unsafe_unretained , inside the NSValue object. More specifically, when you try to return your link (using [myNSValue nonretainedObjectValue]), your application will crash using the EXC_BAD_ACCESS signal if the object has been freed before this time!

In other words, a weak reference is not automatically set to zero while inside an NSValue object. It took me several hours to understand. I worked on this by creating a simple class with a weak ref value.

More beautifully, using NSProxy , we can fully process the wrapper object, as if it were the contained object itself!

 // WeakRef.h @interface WeakRef : NSProxy @property (weak) id ref; - (id)initWithObject:(id)object; @end // WeakRef.m @implementation WeakRef - (id)initWithObject:(id)object { self.ref = object; return self; } - (void)forwardInvocation:(NSInvocation *)invocation { invocation.target = self.ref; [invocation invoke]; } - (NSMethodSignature *)methodSignatureForSelector:(SEL)sel { return [self.ref methodSignatureForSelector:sel]; } @end 
+24
Aug 06 '13 at 12:40
source share

Check the documentation for the value of NSValueWithNonretainedObject:

This method is useful for preventing an object from being saved when it is added to the collection object (for example, an NSArray or NSDictionary instance).

+17
May 2 '11 at 12:30
source share

You do not want to do this! Cocoa Touch a few concepts to send events, you must use the right concept for every occasion.

  • Target action: For user interface controls, such as button clicks. One sender, zero or more recipients.
  • Delegates: for only one sender and one .
  • Notification: for the sender, one or zero or more .
  • KVO: finer grained notifications.

What you should do is see how to use the NSNotificationCenter class. This is the right way to send a notification containing multiple receivers.

+12
Jan 14 '11 at 18:08
source share

I suggest a non-fight-framework and use NSPointerArray using NSPointerFunctionsWeakMemory NSPointerFunctionOption as follows:

 NSPointerArray *weakReferencingArray = [NSPointerArray pointerArrayWithOptions:NSPointerFunctionsWeakMemory]; // NSPointerFunctionsWeakMemory - Uses weak read and write barriers // appropriate for ARC or GC. Using NSPointerFunctionsWeakMemory // object references will turn to NULL on last release. 

Served well in scenarios where I had to create an array of delegates that reference auto-NULL.

+11
Jun 06 '13 at 8:15
source share

This one of NIMBUS would be simpler:

 NSMutableArray* NICreateNonRetainingMutableArray(void) { return (NSMutableArray *)CFArrayCreateMutable(nil, 0, nil); } NSMutableDictionary* NICreateNonRetainingMutableDictionary(void) { return (NSMutableDictionary *)CFDictionaryCreateMutable(nil, 0, nil, nil); } NSMutableSet* NICreateNonRetainingMutableSet(void) { return (NSMutableSet *)CFSetCreateMutable(nil, 0, nil); } 
+3
Mar 18 '12 at 7:13
source share

I found some code snippets from Three20 project on this topic, hope this helps ...

 NSMutableArray* TTCreateNonRetainingArray() { CFArrayCallBacks callbacks = kCFTypeArrayCallBacks; callbacks.retain = TTRetainNoOp; callbacks.release = TTReleaseNoOp; return (NSMutableArray*)CFArrayCreateMutable(nil, 0, &callbacks); } NSMutableDictionary* TTCreateNonRetainingDictionary() { CFDictionaryKeyCallBacks keyCallbacks = kCFTypeDictionaryKeyCallBacks; CFDictionaryValueCallBacks callbacks = kCFTypeDictionaryValueCallBacks; callbacks.retain = TTRetainNoOp; callbacks.release = TTReleaseNoOp; return (NSMutableDictionary*)CFDictionaryCreateMutable(nil, 0, &keyCallbacks, &callbacks); } 
+2
Mar 18 2018-12-18T00:
source share

I found an open source library called XMPPFramewrok

The project has a group delegate solution

https://github.com/robbiehanson/XMPPFramework/wiki/MulticastDelegate

0
Oct 05
source share

How about saving in an array or dictionary

 __weak typeof(pointer) weakPointer = pointer; 
0
Feb 19 '14 at 9:48
source share

Keyword: NSHashTable , document search.

0
Apr 13 '17 at 8:24
source share



All Articles