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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:18:20+00:00 2026-06-07T20:18:20+00:00

I have an NSMutableArray, that should be only modified by the owner object (MyObject).

  • 0

I have an NSMutableArray, that should be only modified by the owner object (MyObject). so the first attemp was to make it a readonly property.
The problem is that this class implements NSCoding, and NSCoding requires the archived objects to be a readwrite property.

Then I thought of having a private property (declared in m file), and have a public method that return my private array. But then this would be a reference to the array, and other classes would be able to modify it.

My Methods should not return a copy of this array either, because I want the other classes to be able to modify each item= in this array, but not the array itself.

QUESTION: How can I have a public property, that is readonly, and at the same time be able to archive and unarchive it?

  • 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-07T20:18:22+00:00Added an answer on June 7, 2026 at 8:18 pm

    There are a few ways to do this, but one reasonable way is to maintain a private, mutable array, then provide a readonly accessor for the public. Then you just have the public accessor return an immutable copy of the internal array. That would look something like this:

    In the .h file:

    @interface MyClass : NSObject
    
    @property (readonly) NSArray *publicArray;
    
    @end
    

    In the .m file:

    @interface MyClass ()
    
    @property NSMutableArray *privateArray;
    
    @end
    
    @implementation MyClass
    
    + (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key
    {
        NSSet *keyPaths = [super keyPathsForValuesAffectingValueForKey:key];
    
        if ([key isEqualToString:@"publicArray"]) {
            keyPaths = [keyPaths setByAddingObject:@"privateArray"];
        }
    
        return keyPaths;
    }
    
    
    @synthesize privateArray = _privateArray;
    
    - (NSArray *)publicArray { return [self.privateArray copy]; }
    
    @end
    

    You could do without the copy if you feel comfortable counting on the compiler to warn about code that attempts to call mutation methods on the result of -publicArray rather than runtime exceptions being thrown. Another caveat is that without a copy, any changes to the private array will be ‘seen’ even in a previously obtained reference to the supposedly immutable array.

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

Sidebar

Related Questions

Scenario- I have a method that returns an object retrieved from an NSMutableArray similar
I have an NSMutableArray object that I want to add custom methods to. I
I have an NSMutableArray that is populated with objects of strings. For simplicity sake
I have a NSMutableArray that I need to search for a string and return
I have a NSMutableArray (called listOfSites) that is created and used in classA.m. I
I have an NSMutableArray called message that I've alloc'd in init and release in
I have been working with NSArray s and NSMutableArray s that store NSDate objects
I have a NSMutableArray and a NSArray . Both consist of elements that are
I have a class that has a method: -(NSInteger) getCityCountForState:(NSString *)state CityArray:(NSMutableArray *)cityArray {
I currently have a function written called saveWorkout that saves an NSMutableArray to another

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.