syedfa
syedfa

Reputation: 2819

Trying to temporarily store objects in an NSMutableArray in a Singleton class for later use in iOS

I have a Singleton class that has two methods:

- (void)saveString:(NSString *)stringObject {

    [[[Singleton sharedInstance] stringArray] addObject:stringObject];
}

- (NSArray *)getArrayContents {

    return [[Singelton sharedInstance] stringArray];
}

Here is the implementation code of my Singleton class:

static Singleton *sharedSingleton = nil;

+ (Singleton *) sharedInstance {

    if (sharedSingleton == nil) {
        sharedSingleton = [[super alloc] init];
    }
    return sharedSingleton;
}

I have two View Controllers (vcA, and vcB) in my application. What I am trying to do is temporarily store the data from vcA, so that the data inside stringArray will be accessible later to vcB.

Here is the code that vcA uses to store the data:

[[Singleton sharedInstance] saveString:stringName];

Later in the lifecycle of the application, vcB calls the Singleton class to retrieve the values from the NSMutableArray:

NSArray *newArray = [[Singleton sharedInstance] getArrayContents];
       for (NSString *test in newArray) {
           NSLog(@"Here are the contents of the array %@", test);
       }

Unfortunately, when I make the call in vcB to print the contents of the Array, there is no output because the array is empty, despite the fact that values are added to the array. What is it I'm doing wrong?

Upvotes: 0

Views: 952

Answers (3)

JanB
JanB

Reputation: 914

I had this problem. My code in the singleton looked like this:

+ (ReportDataList*)sharedDataArray
{
    static dispatch_once_t pred;
    static ReportDataList *shared = nil;
    dispatch_once(&pred, ^{
        shared = [[ReportDataList alloc] init];
        self.rDetailsArray = [[NSMutableArray alloc] init];
    });
    return shared;
}

I had incorrectly initialised the array, so it was emptying it when I created a reference to the singleton later in my code. I removed the array initialisation, which is done in the -(id)init method and it worked fine. So, my code then looked like this:

+ (ReportDataList*)sharedDataArray
{
    static dispatch_once_t pred;
    static ReportDataList *shared = nil;
    dispatch_once(&pred, ^{
        shared = [[ReportDataList alloc] init];
    });
    return shared;
}

- (id)init
{
    self = [super init];
    if (self) {
        self.rDetailsArray = [[NSMutableArray alloc] init];
        [self initWithDummyValues];
    }else{
        NSLog(@"problem initialising array list");
    }
    return self;
}

Upvotes: 1

Akhilrajtr
Akhilrajtr

Reputation: 5182

Try this,

to create Singleton

+(Singleton *)sharedSingleton {

    static dispatch_once_t once;
    static Singleton *sharedSingleton;
    dispatch_once(&once, ^{
        sharedSingleton = [[self alloc] init];
    });
    return sharedSingleton;
}

and the init method of singleton class

- (id)init
{
    self = [super init];
    if (self) {
        //@property stringArray
        self.stringArray = [[NSMutableArray alloc] init];
    }
    return self;
}

Other methods of Singleton

- (void)saveString:(NSString *)stringObject {

    [self.stringArray addObject:stringObject];
}

- (NSArray *)getArrayContents {

    return self.stringArray;
}

Upvotes: 1

Wil Shipley
Wil Shipley

Reputation: 9543

First off, these two methods should probably use self, not sharedInstance:

- (void)saveString:(NSString *)stringObject {

    [[self stringArray] addObject:stringObject];
}

- (NSArray *)getArrayContents {

    return [self stringArray];
}

Second, there’s no point in having a getArrayContents method when you already have stringArray, and get as a prefix is usually reserved for methods that take a parameter to be copied into, anyhow.

Third, I don’t see you initializing stringArray anywhere, so unless there’s code missing, it’s nil and it’s staying nil. Maybe try:

+ (Singleton *) sharedInstance {

    if (!sharedSingleton) {
        sharedSingleton = [[self alloc] init];
        sharedSingleton.stringArray = [NSMutableArray new];
    }
    return sharedSingleton;
}

Assuming stringArray is declared something like:

@property (readwrite, strong) NSMutableArray *stringArray;

Upvotes: 0

Related Questions