1

I know this question has been asked countless of times. But none of them seems to be working! Please help! I am unable to pass arrays from one class to another. Blow are my codes

firstClass has an array declared

#FirstClass.h

- (void)viewDidLoad
{
testArray = [[NSMutableArray alloc] init];
[testArray addObject:@"test"];
[testArray addObject:@"test2"];
[testArray addObject:@"test3"];

//method to pass array

TableViewController *tvc = [[TableViewController alloc] init];
tvc.getData = [[NSMutableArray alloc] init];
tvc.getData = testArray;

NSLog(@"%i", [tvc.getData count]); // returns a value n

Here i get count of 3

}

TableViewController.h

- (void)viewDidLoad
{
[super viewDidLoad];

NSLog(@"%i", [getData count]); // returns a value n 

Here i get 0

//Set the title
self.navigationItem.title = @"Countries";
}

I tried

FirstClass *fc = [[FirstClass alloc] init];
getData = fc.testArray;

Doesn't work as well. I tried writing a method and calling the method from my second class.
Ended up with @interface error.
Sorry, this is my first time on objective c. Am java educated.
Please help!

1 Answer 1

3

For the second problem, why the fc.testArray does not work is that viewDidLoad is called only after the view property is called. (lazy initialization)

So it works if you do like

FirstClass *fc = [[FirstClass alloc] init];
fc.view;
getData = fc.testArray;
Sign up to request clarification or add additional context in comments.

3 Comments

dude thanks!! Ok another question. I ma using a JSON to populate my array in my FirstClass from another method. Thus I am declaring my arrays in that method instead of viewOnLoad. So... how do I solve this?
How about just calling that method: FirstClass *fc = [[FirstClass alloc] init]; getData = [fc anotherMethod];
thx for your help. mind taking a look at my codes? stackoverflow.com/questions/10027336/… this is how I am working with my class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.