Thomas Spade
Thomas Spade

Reputation: 744

Pass Array to NSObject Class

I'm new to iphone app development and I'm stuck on this problem I'm having with the app I'm trying to develop.

I have a datacontroller for populating a tableview. I created it using this tutorial: About Creating Your Second iOS App

I'm trying to pass an array from one of my viewcontrollers that was created from a JSON response.

Here is some code from my viewcontroller.h that needs to pass the array:

@interface ViewController : UIViewController

@property (nonatomic, retain) DataController *Data;
@property (nonatomic, retain) NSMutableArray *array;
@end

viewcontroller.m:

  #import "DataController.h"
    [Data setMasterList: self.array];

DataController.h:

@interface DataController : NSObject

@property (nonatomic, strong) NSMutableArray *masterList;

- (void)setMasterList:(NSMutableArray *)newList;
@end

DataController.m

#import "LoginViewController.h"

    - (void)setMasterList:(NSMutableArray *)newList {
          if (_masterList != newList) {
              _masterList = [newList mutableCopy];
          NSLog("List: %@", newList);


             }
          }

The NSLog message never shows up in the console and the array is nil.

Any help is appreciated.

Thanks.

EDIT:

Here's the updated viewcontroller.m:

Data = [[DataController alloc] init];
[Data setMasterList: self.array];

The datacontroller.m:

- (void)setMasterList:(NSMutableArray *)newList {
      if (_masterList != newList) {
          _masterList = [newList mutableCopy];
      NSLog("List: %@", self.masterList);


         }
}

- (NSUInteger)countOfList {
    NSLog("List: %@", self.masterList);
    return [self.masterList count];
}

The first nslog inside setMasterList returns the correct array values, but the second nslog inside countOfList returns null. The list always returns null anywhere outside of setMasterList. Is it because I'm creating a new instance of the DataController? If so, how else could I pass the array to the datacontroller.

Upvotes: 1

Views: 727

Answers (1)

rptwsthi
rptwsthi

Reputation: 10182

As in first comment Till have suggested, Data must be initialized before calling setMasterList. Such As:

Data = [[DataController alloc] init];
[Data setMasterList: self.array];

Upvotes: 2

Related Questions