Note that there are some explanatory texts on larger screens.

plurals
  1. POCannot access contents of NSMutableArray when calling from a different class
    primarykey
    data
    text
    <p>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. </p> <p>In my .h file I have </p> <pre><code>@interface MainScreen2 : UIViewController @property (nonatomic, strong) NSMutableArray *Judith; </code></pre> <p>and in the viewDidLoad function on the .m file I have:</p> <pre><code>@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]; } </code></pre> <p>this is fine.</p> <p>In a separate class I have:</p> <pre><code>#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]); } </code></pre> <p>and the <code>NSLog</code> 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?</p> <p>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?</p> <hr> <p>EDIT: 30 Apr</p> <p>Using a combination of the suggestions kindly put on this page I have now sorted out the problem and it now works. </p> <pre><code>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); } </code></pre> <p>thanks to all that contributed to the forum post!</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.
    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