Note that there are some explanatory texts on larger screens.

plurals
  1. PORemove pointer to object from NSMutableArray while keeping object?
    primarykey
    data
    text
    <p>I am trying to remove a pointer in an NSMutableArray that points to an object in another array without deleting the object itself. E.g.:</p> <pre><code>// In ViewController.m – Code abridged and somewhat simplified @interface ViewController () @property (nonatomic, strong) NSMutableArray *objectPool; @property (nonatomic, strong) NSMutableArray *objectsOwnedByFriend; @property (nonatomic, strong) NSMutableArray *objectsOwnedByMe; - (void)transferPointerToObjectFromFriendToMe; - (void)copyPointerToObjectFromFriendToMe; @end @implementation ViewController @synthesize objectPool = _objectPool; @synthesize objectsOwnedByFriend = _objectsOwnedByFriend; @synthesize objectsOwnedByMe = _objectsOwnedByMe; - (void)setObjectPool:(NSMutableArray *)objectPool { _objectPool = objectPool; } - (NSMutableArray *)objectPool { if (!_objectPool) _objectPool = [[NSMutableArray alloc] initWithArray:self.objects]; // self.objects is a mutable array containing multiple NSObjects return _objectPool; } - (void)setObjectsOwnedByFriend:(NSMutableArray *)objectsOwnedByFriend { _objectsOwnedByFriend = objectsOwnedByFriend; } - (NSMutableArray *)objectsOwnedByFriend { if (!_objectsOwnedByFriend) { _objectsOwnedByFriend = [[NSMutableArray alloc] init]; [_objectsOwnedByFriend addObjectsFromArray:self.objectPool]; } return _objectsOwnedByFriend; } - (void)setObjectsOwnedByMe:(NSMutableArray *)objectsOwnedByMe { _objectsOwnedByMe = objectsOwnedByMe; } - (NSMutableArray *)objectsOwnedByMe { if (!_objectsOwnedByMe) _objectsOwnedByMe = [[NSMutableArray alloc] init]; return _objectsOwnedByMe; } - (void)transferPointerToObjectFromFriendToMe { [self.objectsOwnedByMe addObject:[self.objectsOwnedByFriend lastObject]]; [self.objectsOwnedByFriend removeLastObject]; } - (void)copyPointerToObjectFromFriendToMe { [self.objectsOwnedByMe addObject:[self.objectsOwnedByFriend lastObject]]; } @end </code></pre> <p>In the above code, when I use transferPointerToObjectFromFriendToMe, removing the last object removes both the pointer to it in self.objectsOwnedByFriend (as I want) and also the object itself in self.objectPool (which I <em>don't</em> want to happen).</p> <p>What I would like is an array (self.objectPool) that contains all of the actual objects and then two mutable arrays (self.objectsOwnedByFriend and self.objectsOwnedByMe) that contains pointers to objects in self.objectPool and the ability to add and remove more pointers referencing objects in self.objectPool to self.objectsOwnedByFriend and self.objectsOwnedByMe.</p> <p><del>Also, when I use either transferPointerToObjectFromFriendToMe or copyPointerToObjectFromFriendToMe, the object doesn't seem to be added properly, as a subsequent check via self.objectsOwnedByMe.count results in 0 instead of 1.</del><br />SOLUTION = My lazy instantiation for self.objectsOwnedByMe was missing in my original code :S<br />I was able to check whether self.objectsOwnedByMe was properly created via:</p> <pre><code>NSLog(@"self.objectsOwnedByMe = %@", self.objectsOwnedByMe); </code></pre> <hr> <p>** My first StackOverflow question! ** I hope I was clear...couldn't find a a similar question so apologies if I missed an old thread. Let me know if you need more info to diagnose. (I am trying to learn Obj-C.)</p>
    singulars
    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.
 

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