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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:22:10+00:00 2026-06-11T21:22:10+00:00

I am declaring a property in my class in header file; @property (readonly) NSArray

  • 0

I am declaring a property in my class in header file;

@property (readonly) NSArray *pages

That’s how I want it to be exposed publicly. Internally though, I am going to allocating it as NSMutableArray so I can add/remove stuff from it. But to do that, I will have to type cast every time. Is there a better way to do this?
Thanks

  • 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-11T21:22:12+00:00Added an answer on June 11, 2026 at 9:22 pm

    There isn’t a solution for this. You have to cast every time, or use different properties. Here is a sample for the second approach:

    @interface MyClass : NSObject
    @property (nonatomic, strong, readonly) NSArray *pages;
    -(void)addObject:(id)obj;
    @end
    
    @interface MyClass()
    @property (nonatomic, strong, readwrite) NSMutableArray *mPages;
    @end
    
    @implementation MyClass
    -(id) init {
        self = [super init]
        if (self){
            _mPages = [NSMutableArray array];
        }
        return self;
    }
    -(NSArray*)pages {
        return [NSArray arrayWithArray:self.mPages];
    }
    -(void)addObject:(id)obj {
        [self.mPages addObject:obj];
    }
    @end
    
    
    int main(int argc, char *argv[]) {
        @autoreleasepool {
            MyClass *m = [MyClass new];
            [m addObject:@"x"];     // the collection is mutable
            NSLog(@"%@",[m pages]); // but only accessible as an immutable copy
        }
    }
    

    This will be expensive if you access the collection frequently, and may be out of sync with the internal mutable collection (which may be mutated while you iterate on the copy).

    Copying can be avoided returning the internal mutable instance (NSMutableArray) disguised as an immutable class (NSArray), but that incurs the following risks:

    • The client could cast to mutable and change it.
    • The internal copy could be mutated. This will crash the application if you are iterating, or may cause an index out of range exception.

    Note that the following idiom doesn’t solve the problem:

    @interface MyClass : NSObject
    @property (nonatomic, strong, readonly) NSArray *pages;
    @end
    
    @interface MyClass()
    @property (nonatomic, strong, readwrite) NSMutableArray *pages;
    @end
    

    This lets you set the variable, but not use it as a different class than the one declared in the interface. In other words, it forces you to cast on every use:

    [(NSMutableArray*)pages addObject:@"x"];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm declaring an NSString property in a class and objective-c is complaining that: NSString
Declaring a property in a derived class that matches the name of a property
I am declaring a List object property with: @XmlRootElement(namespace = ...) @XmlType public class
I want to call a partial method from outside of the declaring class. This
Suppose you have class B with lazily loaded property c . And that this
If I make a class member private, and then I want to acess that
I have a property in a code-behind class to which I want to bind
Lets say that i have a class with some instance variables and i want
I got two properties initialized in header file: @property (readwrite, assign) int Figure1; @property
Stupid question, but why do we need to use 'retain' when declaring a property?

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.