Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7933211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:15:35+00:00 2026-06-03T21:15:35+00:00

I am trying to remove a pointer in an NSMutableArray that points to an

  • 0

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.:

// 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

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 don’t want to happen).

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.

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.
SOLUTION = My lazy instantiation for self.objectsOwnedByMe was missing in my original code :S
I was able to check whether self.objectsOwnedByMe was properly created via:

NSLog(@"self.objectsOwnedByMe = %@", self.objectsOwnedByMe);

** 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.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T21:15:37+00:00Added an answer on June 3, 2026 at 9:15 pm

    Typo 😛 Sorry peeps. In my actual code in Xcode I had:

    - (void)setObjectPool:(NSMutableArray *)objectPool
    {
        _objectPool = objectPool;
    }
    
    - (NSMutableArray *)objectPool
    {
        if (!_objectPool) _objectPool = [[NSMutableArray alloc] initWithArray:self.objects];
        return _objectsOwnedByFriend;
    }
    

    I think my mistake is super obvious (and if not, the mistake was that my getter for objectPool was returning _objectsOwnedByFriend…copy/paste error that I somehow missed).

    Everything works now!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to convert some code that is meant to remove all non-numeric
I was trying to remove my recent connection list by deleting the mru.dat (in
I am trying to remove spaces from the end of a char array (string).
Ok so i posted earlier about trying to (without any prebuilt functions) remove additional
I am trying to create a property that will make a pointer (byte*) from
I am trying to find a way to take an array of keywords that
I'm trying to remove some confusion with pointer to structures which are used as
I am trying to remove a pending changelist in perforce. All the files (20
I'm trying to remove some items of a dict based on their key, here
I'm trying to remove a color from a picture imported (JPG) in Flash CS4

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.