0

I have four views, with four arrays. As soon as I navigate from one view, I add that particular array to my master array.

Initially this master array has nothing, and is allocated when the app runs using 'init' method. It does not have a view and it is an array from a subclass of NSObject.

Finally the master array should consist of {viewarray1, viewarray2, viewarray3, viewarray4}.

Each array is added to the master array when navigating to the next view.

So is there anything wrong in my implementation ? Is it okay to allocate masterArray in the init method? Each time I add an object to masterArray, I NSLog it and it displays (null)

How can I have the master array retain values for the whole app??

Some Information on the Code:

I initialize my master array in another class, in the -(init) method

masterArray = [[NSMutableArray alloc] init ];

While adding an object to MasterArray from another view, I reference that class, and I create an object for the class, and add it as a property and synthesize it. I then use

[self.thatClassObject.masterArray addObject:self.viewArray1];
11
  • 1
    Post some of your code please, what you have listed currently describes what you are trying to accomplish, but not what you've done to implement it. Commented Jun 29, 2011 at 18:13
  • @Perception: The code is simple. I just reference the array from another class and try to add an object to it. Commented Jun 29, 2011 at 18:15
  • You might want to look into UINavigationController. It stores an array of viewControllers that have been pushed onto the stack. Commented Jun 29, 2011 at 18:32
  • @Andrew: I dont think this is an array of viewControllers I am talking about. It is about an array of data/values Commented Jun 29, 2011 at 18:33
  • is the data from each view existing when you start? if so, you might be able to grab the arrays from your app delegate using property values. for example, you could have masterArray = [[NSArray alloc] initWithObjects:view1.array, view2.array, view3.array, view4.array, nil];. you'd need to have array specified as @property (nonatomic, retain) NSArray *array; in each view's header, then synthesized as well. again, this only works if you have the arrays there initially and they aren't created during run-time Commented Jun 29, 2011 at 19:41

1 Answer 1

1

There are two ways you could go about initializing it that I can think of offhand. First, you could alloc/init the array in the app delegate. This will ensure it's created before the rest of the views get a chance to add to it.

EDIT: There's only really one way I can think to do this as Josh Caswell pointed out a good fact that class initializations won't work for this situation. You're best off calling alloc/init for the array either in the app delegate or whichever view is made key window first. You'll have to reference it to the other classes from there

Sign up to request clarification or add additional context in comments.

5 Comments

This doesn't work -- no instance has any reference to the array, and the array is being leaked immediately. In addition, you need to check whether +initialize has already run.
Good point. For registering a dictionary of user default info, a class initialization works fine, but that is handled differently than trying to use an array amongst different classes. I'm glad you spotted that. I'll be sure to edit it so that only the app delegate option is there and not the class initialization. Thanks again for the good catch
so yes, that class thing did not work. I had wasted so many hours because of trying to reference classes, and it did not work in the app delegate either. I had to declare the master array in APP initialization and reference it using the delegate. Thanks a lot JOSH CASWELL FOR YOUR ACTIVE PARTICIPATION. AND @Slev:: (as always.. ) Thanks man !!
Just out of curiosity. How do you think Singleton classes would help in these cases ?
A Singleton class could certainly work in this situation. I've never yet used any myself, but from what I read on them while learning Objective-C, they should be a good option for this sort of purpose

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.