Question

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.

Was it helpful?

Solution

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
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top