Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The way I've solved this problem in the past is to use a subclass of NSProxy to break the cycle. I'll have an object that stores a weak reference to one of the arrays and passes all messages except memory management ones through to it.</p> <pre><code> ┌──── NSArray A &lt;────┐ │ │ │ │ v weak │ ACWeakProxy ┈ ┈ ┈ ┈ ┈ &gt; NSArray B </code></pre> <hr> <pre><code>@interface ACWeakProxy : NSProxy { id _object; } @property(assign) id object; - (id)initWithObject:(id)object; @end </code></pre> <hr> <pre><code>@implementation ACWeakProxy @synthesize object = _object; - (id)initWithObject:(id)object { // no init method in superclass _object = object; return self; } - (BOOL)isKindOfClass:(Class)aClass { return [super isKindOfClass:aClass] || [_object isKindOfClass:aClass]; } - (void)forwardInvocation:(NSInvocation *)invocation { [invocation setTarget:_object]; [invocation invoke]; } - (NSMethodSignature *)methodSignatureForSelector:(SEL)sel { return [_object methodSignatureForSelector:sel]; } @end </code></pre> <hr> <p>Then your code becomes</p> <pre><code>NSMutableArray * A = [[NSMutableArray alloc] initwithObjects:@"one", nil]; NSMutableArray * B = [[NSMutableArray alloc] initwithObjects:@"two", nil]; [A addObject:B]; ACWeakProxy * proxy = [[ACWeakProxy alloc] initWithObject:A]; [B addObject:proxy]; [proxy release]; // will print "two" NSLog(@"%@", [[[B objectAtIndex:1] objectAtIndex:1] objectAtIndex:0]); </code></pre> <p>It is, however, up to you to make sure your weak reference doesn't vanish while you are still using it.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload