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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:50:28+00:00 2026-06-04T13:50:28+00:00

Say I have this Class @interface CustomClass : NSObject @property (nonatomic, strong) NSArray *

  • 0

Say I have this Class

@interface CustomClass : NSObject

@property (nonatomic, strong) NSArray * nicestArrayEver;

@end

And I want to create a subClass of CustomClass, but here is the catch

@interface ASubClassCustomClass : CustomClass

@property (nonatomic, strong) NSMutableArray * nicestArrayEver;

@end

The issue as you can imagine is that when I initialize ASubClassCustomClass and call it’s super initializer (since there is other properties required) the inmutable nicestArrayEver is created.. how can I avoid it’s creation so I can set the mutable one?

Note: This is just an example, the real implementation calls a heavy to create and really customized subclass (is not an NSArray).

  • 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-04T13:50:29+00:00Added an answer on June 4, 2026 at 1:50 pm

    You can make it work, by using different backing variables, when synthesizing it looks like this: @synthesize nicestArrayEver = nicestArrayEverSubClass_;

    #import <Foundation/Foundation.h>
    
    @interface CustomClass : NSObject
    
    @property (nonatomic, strong) NSArray * nicestArrayEver;
    
    @end
    
    @implementation CustomClass
    @synthesize nicestArrayEver ;
    
    -(id)init
    {
        if (self = [super init]) {
            nicestArrayEver = [[NSArray alloc] init];
        }
        return self;
    }
    @end
    
    @interface ASubClassCustomClass : CustomClass
    
    @property (nonatomic, strong) NSMutableArray * nicestArrayEver;
    
    @end
    
    @implementation ASubClassCustomClass
    @synthesize nicestArrayEver = nicestArrayEverSubClass_;
    
    -(id)init{
        if (self = [super init]) {
            nicestArrayEverSubClass_ = [[NSMutableArray alloc] init];
        }
        return self;
    }
    @end
    
    
    
    int main(int argc, const char * argv[])
    {
    
        @autoreleasepool {
    
            CustomClass *c1 = [[[CustomClass alloc] init] autorelease];
            ASubClassCustomClass *c2 = [[[ASubClassCustomClass alloc] init] autorelease];
    
            NSLog(@"%@", NSStringFromClass([[c1 nicestArrayEver] class]));
            NSLog(@"%@", NSStringFromClass([[c2 nicestArrayEver] class]));
    
        }
        return 0;
    }
    

    output

    2012-05-27 01:59:16.221 NicestArray[2312:403] __NSArrayI
    2012-05-27 01:59:16.225 NicestArray[2312:403] __NSArrayM
    

    Another approach could be to have 2 init methods in the base class, one, that will instantiate the property and one, that won’t, but leaves that task for the child class — this will prevent you from creating expensive objects just to throw them away.
    Now the base class could get instantiated directly with the second init and go to a false state. You can avoid this by checking the self class type with isMemberOfClass:, and throw an error, if the class type is the base class.

    @interface CustomClass : NSObject
    
    @property (nonatomic, strong) NSArray * nicestArrayEver;
    -(id)initWithoutArray;
    @end
    
    @implementation CustomClass
    @synthesize nicestArrayEver ;
    
    -(id) initWithoutArray
    {
        if (self = [super init]) {
            if ([self isMemberOfClass:[CustomClass class]]) {
                [NSException raise:@"AbstractMethodCall" format:@"%@ should be called only from Subclasses of %@", NSStringFromSelector(_cmd), NSStringFromClass([self class])];
            }
        }
        return self;
    }
    
    
    -(id)init
    {
        if (self = [super init]) {
            nicestArrayEver = [[NSArray alloc] init];
        }
        return self;
    }
    @end
    
    @interface ASubClassCustomClass : CustomClass
    
    @property (nonatomic, strong) NSMutableArray * nicestArrayEver;
    
    @end
    
    @implementation ASubClassCustomClass
    @synthesize nicestArrayEver = nicestArrayEverSubClass_;
    
    -(id)init{
        if (self = [super initWithoutArray]) {
            nicestArrayEverSubClass_ = [[NSMutableArray alloc] init];
        }
        return self;
    }
    
    @end
    
    
    
    int main(int argc, const char * argv[])
    {
    
        @autoreleasepool {
    
            CustomClass *c1 = [[[CustomClass alloc] init] autorelease];
            ASubClassCustomClass *c2 = [[[ASubClassCustomClass alloc] init] autorelease];
    
            NSLog(@"%@", NSStringFromClass([[c1 nicestArrayEver] class]));
            NSLog(@"%@", NSStringFromClass([[c2 nicestArrayEver] class]));
    
            //this works, as it is the subclass
            ASubClassCustomClass *shouldWork = [[[ASubClassCustomClass alloc] init] autorelease];
    
            // ouch!
            CustomClass *shouldCrash = [[[CustomClass alloc] initWithoutArray] autorelease];
    
        }
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have this interface and class: interface IListener { void f(); void
Say you have this: class Question < ActiveRecord::Base has_many :answers end class Answer <
This is a little contrived, but say I have a class interface like this:
Say I have the following class: @interface Frob : NSObject { NSData *data; }
Let's say I have this class and its subclass @interface MySuperClass - (void)open:(id)type value:(id)value;
Say I have this class: class myclass { public int Field1{ get; set; }
Say I have this class: public class Account { public int AccountID { get;
say I have this class: class animal { function noise() { print 'woof'; }
Lets say I have this class in foobar-shared.lib: class FooBar { std::string m_helloWorld; }
Lets say have this immutable record type: public class Record { public Record(int x,

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.