Note that there are some explanatory texts on larger screens.

plurals
  1. POObjective C Singleton Class
    primarykey
    data
    text
    <p>I have created a singleton class in Objective C. Like the one below:</p> <pre><code>@interface SingletonClass : NSObject{ NSMutableArray *instanceArray; } @property(nonatomic,retain)NSMutableArray *instanceArray; +(SingletonClass*)sharedInstance; @end @implementation SingletonClass @synthesize instanceArray; static SingletonClass *sharedInstance =nil; +(SingletonClass*)sharedInstance { if(sharedInstance==nil){ sharedInstance=[[super allocWithZone:NULL]init]; } return sharedInstance; } -(id)init{ self=[super init]; if(self){ instanceArray=[[NSMutableArray alloc]init]; [instanceArray addObject:@"One"]; [instanceArray addObject:@"Two"]; } return self; } +(id)allocWithZone:(NSZone *)zone{ return [self sharedInstance]; } @end </code></pre> <p>I know it can be accessed from anywhere with the following piece of code:</p> <pre><code> SingletonClass *singletonObject=[SingletonClass sharedInstance]; </code></pre> <p>And the instanceArray can be accessed anywhere by singletonObject.instanceArray.</p> <p>Now my question is, is is possible to modify the array by adding new objects to it ? Will that be persisted ? Because i tried to add an object from one class</p> <pre><code> [singletonObject.instanceArray addObject:@"Three"]; </code></pre> <p>When i checked the array contents from another class, the array consisted of only two values which are initialized by default. The value which i added from another class didnt show up. What could be the problem ? Am I missing something here ? Please help.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload