2

I am new to Obj-C and Cocoa and trying to figure out how to do a sorted linked list of objects pre and post ARC.

My class would be something like

@interface Node : NSObject
{
    int value;
    NSValue *item;
    Node *next;
}

property (strong, non atomic) value;
...

Entering a new item is done by scanning the list of Nodes and finding the insertion point by comparing the Node's value property. Where I get in trouble is when I want to remove an item from the list. My code is something like

...
Node *prevPtr = nil;
Node *curPtr = head;
while (curPtr != nil) {
    if (some-condition) {
        prevPtr.next = curPtr.next;
        [curPtr release]; // cannot do with ARC
    }
}
  1. Is this coding pattern compatible with Cocoa?
  2. Under ARC, where/when would my object get released/deallocated?
5
  • Why do you feel that you need to write your own linked list instead of simply using an NSMutableArray? Commented Jan 4, 2012 at 5:53
  • Maybe because NSMutableArray uses the same type of backing data structure as an ArrayList in Java, making arbitrary insertions and deletions inefficient? I'm not saying that it does, but that could be one reason for wanting to implement a custom linked-list. Commented Jan 4, 2012 at 6:21
  • 3
    NSArray has a very...interesting implementation, ridiculousfish.com/blog/posts/array.html is very good reading. Best quote as it relates to this convo "Don't second guess Apple, because Apple has already second guessed YOU. In a good way, of course." Commented Jan 4, 2012 at 16:54
  • 1
    Actually I did end up using NSMutableArray, however my question still stands as a general thought exercise. I wonder where ARC will place the release and what it uses as a hint to do so. Commented Jan 5, 2012 at 20:56
  • Game trees do not get tamed by NSArrays Commented Nov 24, 2014 at 18:41

1 Answer 1

6

The code would be identical except in ARC you would just remove the 'release' line. The compiler will implicitly add it for you as part of the ARC compile. Trust it to do the right thing.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.