views:

198

answers:

1

Hello. I am making an iPhone app that uses a few NSArrays. Right now I have to generate the arrays in each method. I know there has to be an more efficient way of doing this, like having the NSArrays created during initialization and then being available to all methods after that. The problem is, that when I create the NSArrays in the ViewDidLoad method, when I try to call them in other methods, I get an error stating that they are not recognized. Perhaps I am trying to initialize the NSArrays incorrectly or maybe in the wrong spot? Any info on this would be appreciated. Thank you for your time.

+1  A: 

You should declare your array as a property. Synthesize it and initialize in your ViewDidLoad Method.

i.e. the header

@interface AddFriendViewController : UIViewController {
    NSArray *myFriends;
}

@property (nonatomic, retain) NSarray *myFriends;

@end

the implementation:

@synthesize myFriends;

- (void)viewDidLoad {
    [super viewDidLoad];
    // init and alloc your myFriendsArray here
}
Henrik P. Hessel
Yes. This did it. Thank you. I had attempted this before, but did not properly set it up and I mistakenly thought it wouldn't work.
MarcZero