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 8906479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:40:00+00:00 2026-06-15T02:40:00+00:00

I just noticed that calling addObject: on an NSMutableArray doesn’t access that array’s setter.

  • 0

I just noticed that calling addObject: on an NSMutableArray doesn’t access that array’s setter.

E.g., for NSMutableArray self.myArray, [self.myArray addObject:object] does not use [self setMyArray:array] to add the object.

Previously I have been using custom setters and getter to check assignment before assigning; e.g., if I wanted an array that only accepted objects of class MyClass, I would do the following:

- (void)setMyArray:(NSMutableArray *)myArray
{
    for (id object in myArray)
    {
        if (![object isKindOfClass:[MyClass class]]) return;
    }
    _myArray = myArray;
}

- (NSMutableArray *)myArray
{
    if (!_myArray) _myArray = [[NSMutableArray alloc] init];
    _myArray = myArray;
}

How do I go about achieving this same functionality when changing the array via addObject:, removeObject:, and other similar functions that may circumvent the setter?

  • 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-15T02:40:02+00:00Added an answer on June 15, 2026 at 2:40 am

    Generally this kind of problem is the reason why NSMutableArray is usually avoided in preference of NSArray.

    This is the simple solution, use NSArray instead of NSMutableArray:

    self.myArray = [self.myArray arrayByAddingObject:foo];
    

    However, if the array is really big that will cause performance issues. Then you’ve got two options:

    • you can have your own addObjectToMyArray: method in your class and always use that
    • you can create an NSArrayController and use that to access your array. It will implement key value observing and bindings and all of that stuff.

    NSMutableArray is designed to perform addObject: with as few CPU instructions as possible and therefore does not proved any way for external code to be notified that the object was added. You have to have some other class wrapped around it.

    Do not try to subclass NSMutableArray, because it is a “class cluster” making subclasses extremely complicated.

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

Sidebar

Related Questions

I just noticed that moving the mouse cursor across a tooltip does not seem
I just noticed that java.beans.Introspector getBeanInfo does not pickup any superinterface's properties. Example: public
I just noticed that say http://s7.addthis.com/js/250/addthis_widget.js#pub=PUBID does the equivalent of http://s7.addthis.com/js/250/addthis_widget.js?pub=fct-250 but is much
I have just noticed that my ViewController does not call init (See below) when
I just noticed that we can access c++ static member function by member-selection operator
I just noticed that for vector push_back it is push back a reference to
I just noticed that if I have an identity column in a table, when
I've just noticed that if I do a MySQL request like this one: SELECT
I've just noticed that Webkit now has some support regarding the CSS Values and
So I've just noticed that on my iPod Touch, when my app triggers a

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.