First I'll make a getInstance methods like:
@implementation SinglentonObject{
NSMutableArray *array;
}
+ (id)getInstance {
static SinglentonObject *instance = nil;
@synchronized(self) {
if (instance == nil) {
instance = [[SinglentonObject alloc] init];
}
return instance;
}
}
-(void) setStock:(NSNumber *)stock{
// Ensure we are using the shared instance
if(!array){
array = [@[] mutableCopy];
}
[array addObject:stock];
}
- (NSMutableArray *) getStockArray
{
return stockArray;
}
And in someMethod you have to change i by x:
-(void) write
{
for(int x=0;x<=1000;x++){
[[SinglentonObject getInstance] setStock:@x];
}
}
For read this:
- (void) read
{
for (NSNumber *currentStock in [[SinglentonObject getInstance] getStockArray]) {
NSLog(@"%d", [currentStock intValue]);
}
}
Remember to modify every method of getInstance as class methods.I recommend you that you read some Singlenton pattern article like http://www.johnwordsworth.com/2010/04/iphone-code-snippet-the-singleton-pattern/.