I want to have an array that is accessible throughout my Objective-C project whose contents I can change where necessary. My problem is that when I call the array from a different class I always get null, and that’s not what I want.
In my .h file I have
@interface MainScreen2 : UIViewController
@property (nonatomic, strong) NSMutableArray *Judith;
and in the viewDidLoad function on the .m file I have:
@interface MainScreen2 ()
@end
@implementation MainScreen2
@synthesize Judith;
- (void)viewDidLoad
{
self.Judith = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9", nil];
[super viewDidLoad];
}
this is fine.
In a separate class I have:
#import "MainScreen2.h"
@interface NewGame ()
@end
- (void)viewDidLoad
{
MainScreen2 *testJudith;
NSMutableArray *testJudithArray = [[NSMutableArray alloc]init];
testJudithArray = [testJudith.Judith mutableCopy];
NSLog(@"Jud test: %@", [testJudith.Judith objectAtIndex:1]);
}
and the NSLog returns null for this point. Is this because when I am calling the Judith array from the MainScreen.h file it is empty at this point because it is not yet loaded?
If so, can anyone help me with where I should be putting the array so when I call it I retain it’s original contents?
EDIT: 30 Apr
Using a combination of the suggestions kindly put on this page I have now sorted out the problem and it now works.
I changed the code to the following:
- (void)viewDidLoad
{
MainScreen2 *testJudith = [[MainScreen2 alloc]init];
[testJudith viewDidLoad];
NSString *test = [testJudith.Judith objectAtIndex:1];
NSLog(@"Jud test: %@", test);
}
thanks to all that contributed to the forum post!
viewDidLoadis not the same asinit…you either move the allocation of the NSArray ininitor you call[testJudith viewDidLoad];