1

I currently build an NSMutableArray in Class A.m within the ViewDidLoad Method.

- (void)viewDidLoad
{
    [super viewDidLoad];

    //Question Array Setup and Alloc
    stratToolsDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:countButton,@"count",camerButton,@"camera",videoButton,@"video",textButton,@"text",probeButton,@"probe", nil];
    stratTools = [[NSMutableArray alloc] initWithObjects:@"Tools",stratToolsDict, nil];
    stratObjectsDict = [[NSMutableDictionary alloc]initWithObjectsAndKeys:stratTools,@"Strat1",stratTools,@"Strat2",stratTools,@"Strat3",stratTools,@"Strat4", nil];
    stratObjects = [[NSMutableArray alloc]initWithObjects:@"Strategies:",stratObjectsDict,nil];
    QuestionDict = [[NSMutableDictionary alloc]initWithObjectsAndKeys:stratObjects,@"Question 1?",stratObjects,@"Question 2?",stratObjects,@"Question 3?",stratObjects,@"Question 4?",stratObjects,@"Question 5?", nil];


    //add strategys to questions
    QuestionsList = [[NSMutableArray alloc]init];
    for (int i = 0; i < 1; i++) {
        [QuestionsList addObject:QuestionDict];
    }
    NSLog(@"Object: %@",QuestionsList);

At the end of this method you will see QuestionsList being initialized and now I need to send this Array to Class B.

So I place its setters and getters using the @property and @Synthesize method. Class A.h

@property (retain, nonatomic) NSMutableDictionary *stratToolsDict;
@property (retain, nonatomic) NSMutableArray *stratTools;
@property (retain, nonatomic) NSMutableArray *stratObjects;
@property (retain, nonatomic) NSMutableDictionary *QuestionDict;
@property (retain, nonatomic) NSMutableArray *QuestionsList;

Class A.m

@synthesize QuestionDict;
@synthesize stratToolsDict;
@synthesize stratObjects;
@synthesize stratTools;
@synthesize QuestionsList;

I use the property method because I am going to call this variable from Class B and want to be able to assign it to another NSMutableArray.

I then add the @property and @class for Class A to Class B.h as well as declare the NSMutableArray in the @interface.

#import "Class A.h"

    @class Class A;

    @interface Class B : UITableViewController<UITableViewDataSource, UITableViewDelegate>{
        NSMutableArray *QuestionList;
        Class A *arrayQuestions;
      }

    @property Class A *arrayQuestions;

Then I call NSMutableArray from Class A in the Class B.m

-(id)initWithStyle:(UITableViewStyle)style
{
    if ([super initWithStyle:style] != nil) {

        //Make array
        arrayQuestions = [[Class A alloc]init];
        QuestionList = arrayQuestions.QuestionsList;

Right after this I Log the NSMutableArray to view values and check that they are there and it returns NIL.

//Log test
NSLog(@"QuestionList init method: %@",QuestionList);

Info about Class B- Class B is a UIPopOverController for Class A, Class B has one View which holds a UITableView which I have to populate the results of Class A's NSMutableArray.

Why is the NsMutableArray coming back as NIL? Ultimately would like some help figuring it out as well, it seems to really have me confused. Help is greatly appreciated!!

Girish
  • 4,692
  • 4
  • 35
  • 55
Keeano
  • 309
  • 8
  • 33
  • Your error is in believing that you can alloc/init an instance of a class and it will miraculously gain access to values in other instances of that class. – Hot Licks Jun 30 '13 at 23:15
  • Should i use the singleton method towards solving this? for some reason this has really got me stumped. @HotLicks – Keeano Jul 01 '13 at 00:35
  • 2
    @KeeanoMartin Singletons don't solve problems, they create global state. – CodaFi Jul 01 '13 at 01:08
  • 1
    Usually when you use a singleton for something like this it's a sign of poor design. Pass addressability between the object instances when you create them. Usually there's a path that makes sense. – Hot Licks Jul 01 '13 at 02:35
  • Is it because of the approach of Global Access that it gives it poor design? Should i just make setters and getters for the value in Class A and then access the value that way in Class B? @HotLicks – Keeano Jul 01 '13 at 02:42
  • You really need to sit down and think about your design. Generally objects have a sort of tree relationship to other objects, and if you make the data flow along the tree branches it will make more sense and you will control the data better. There are occasions where you need to communicate across a big gap and need some sort of global addressability, but that's the exception. (Just be sure you're thinking about the relationship between OBJECTS, not classes.) – Hot Licks Jul 01 '13 at 02:56

1 Answers1

1

To answer your first question you have a NIL property "suddenly" because what you're doing is instantiating a non-visible version of your Class B even though your new XIB/storyboard frame is already in view in a different part of memory.

If you're using storyboards I would recommend taking advantage of the prepareForSegue method as called out by @HotLicks. Personally I like this answer for passing data forward and backward between view controllers.

If you are NOT using storyboards you will need to start pushing your new XIB manually to get the data from A to B in an object-oriented friendly manner.

A singleton instance certainly will take care of the issue though from an object-oriented purist perspective it is frowned upon. If you go down this route you need to make sure you use dispatch_once of you're going to have the potential for view controllers re-instantiating your useful data errantly.

There are some other ways as well but that should help get the ball rolling. Good luck.

Community
  • 1
  • 1
Dan
  • 5,153
  • 4
  • 31
  • 42
  • Thank you for answering this for me, I am not using StoryBoards. So i should just go down the road of Setting and Getting the value? As in most object oriented languages. But using the singleton method will put global variables that can be accessed and we dont want that.....Correct? – Keeano Jul 01 '13 at 02:39
  • It depends on the "we". I'll use them for complex data classes but it's really up to you in the end. It's your code :) – Dan Jul 01 '13 at 02:57
  • Okay, thats true haha Thanks! I tried the singleton method but im still getting a NIL response....If i opened a shat, would you help me with that? – Keeano Jul 01 '13 at 03:09
  • here is the room:http://chat.stackoverflow.com/rooms/32656/help-with-array-singleton – Keeano Jul 01 '13 at 03:21
  • I'm glad the solution above works. Good luck with the rest of the project. – Dan Jul 03 '13 at 12:19