Well, I try to avoid global variables, so I read about singleton classes. This is an attempt to install and read the modified array, but the result will be null.
@interface Content : NSObject {
NSMutableArray *contentArray;
}
+ (Content *) sharedInstance;
- (NSMutableArray *) getArray;
- (void) addArray:(NSMutableArray *)mutableArray;
@end
.
@implementation Content
static Content *_sharedInstance;
+ (Content *) sharedInstance
{
if (!_sharedInstance)
{
_sharedInstance = [[Content alloc] init];
}
return _sharedInstance;
}
- (NSMutableArray *) getArray{
return contentArray;
}
- (void) addArray:(NSMutableArray *)mutableArray{
[contentArray addObject:mutableArray];
}
@end
And in ViewController, I added #import "Content.h", where I am trying to name it:
NSMutableArray *mArray = [NSMutableArray arrayWithObjects:@"test",@"foo",@"bar",nil];
Content *content = [Content sharedInstance];
[content addArray:mArray];
NSLog(@"contentArray: %@", [content getArray]);
source
share