-1

jeoData is a singleton...the table displays just fine, NSLog verifies the correct row is selected, however, jeoData.crewList.count returns zero after adding the object to the NSMutableArray...

in the jeoData singleton, employeeList is initialized identically to crewList.

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    Employee *employee = [jeoData.employeeList objectAtIndex:indexPath.row];
    [jeoData.crewList addObject:employee];

    NSLog(@"SelectCrewVC: added: %@ %@", employee.firstName, employee.lastName);
    NSLog(@"        total selected: %lu", (unsigned long)jeoData.crewList.count);    
}

NSLog: 
2014-10-17 15:09:41.590 SaveAndLoad[98371:414247] SelectCrewVC: added: Jacob Johnson
2014-10-17 15:09:41.591 SaveAndLoad[98371:414247]         total selected: 0

...even the didDeselectRow selects the correct object to display, but I cannot figure out why I can't get the object into the crewList array.

Any help would be greatly appreciated, because I'm stumped.

EDIT: adding code as per request

JEOData.m (singleton... where init occurs)

@implementation JEOData

@synthesize employeeList;
@synthesize leaseList;
@synthesize crewList;
@synthesize workReport;

+(id)sharedManager {
    static JEOData *sharedJEOData = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^ {
        sharedJEOData = [[self alloc] init];
    });
    return sharedJEOData;
}

-(id)init {
    if (self = [super init]) {
        employeeList = [[NSMutableArray alloc] init];
        leaseList = [[NSMutableArray alloc] init];
        crewList = [[NSMutableArray alloc] init];
        workReport = [[NSMutableArray alloc] init];
    }
    return self;
}

@end

jeoData is declared in the @interface of the header file of the View Controller

@interface SelectCrewViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
    JEOData *jeoData;
    NSMutableArray *crewList;  <---created only in troubleshooting, unused now
}

ViewDidLoad:

- (void)viewDidLoad {
[super viewDidLoad];

// Do any additional setup after loading the view.
jeoData = [JEOData sharedManager];

self.crewSelectTable.allowsMultipleSelection = YES;

NSLog(@"Num of employees total        :%lu", (unsigned long)jeoData.employeeList.count);
NSLog(@"       employees in crew list: %lu", (unsigned long)jeoData.crewList.count);
}

NSLog returns:

2014-10-17 15:09:39.473 SaveAndLoad[98371:414247] Num of employees total        :3
2014-10-17 15:09:39.474 SaveAndLoad[98371:414247]        employees in crew list: 0

So, you can see that jeoData.employeeList contains the right objects, as does jeoData.leaseList. I know that DidSelectRows is being called because of the NSLog, so I know that the Employee object is well and alive, yet it will not let me add the little bugger to the crewList mutable array.

I am fairly certain that I am just overlooking something, yet I am at a loss to see what it is.

Please, help us, Obi-wan Kenobi, you're our only hope.

  • I assume that, at some point, you initialized crewList? – Doc Oct 17 '14 at 20:31
  • It is initialized along w employeeList via the singleton, and employeeList returns data in the same method. – Jacob Johnson Oct 17 '14 at 20:39
  • 1
    Where have you verified that both `jeoData` and `jeoData.crewList` are both not `nil`? – rmaddy Oct 17 '14 at 20:50
  • Jeodata is not nil, the employeeList is able to be accessed. The crewList should be nil until the first didselectrow method is invoked. – Jacob Johnson Oct 17 '14 at 21:32
  • @JacobJohnson It shouldn't be nil, it should be empty - you don't initialize it in didSelectRow. That's what I meant by asking if you initialized it. – Doc Oct 17 '14 at 21:55
  • Pardon the amateur question, I'm self taught and new at this, but if it is initialized in the same singleton, along with employeeList and another array that is working, it should be empty, not nil (my mistake saying that), correct? – Jacob Johnson Oct 17 '14 at 22:22
  • 1
    Print the crewList in the NSLog statement, and you'll know. NSLog(@"crewList:%@", jeoData.crewList); – fishinear Oct 17 '14 at 22:33
  • @JacobJohnson It would help if you posted the code where the `crewList` is supposedly initialized. – rmaddy Oct 17 '14 at 22:59
  • Added some more code... still clueless why this won't work. Might try creating a helper method that adds the object to the array, and see if that has any effect, but that's the only guess I have. – Jacob Johnson Oct 18 '14 at 01:51
  • @fishinear .. tried your idea, returned (null) ...3 lines after the addObject line, 2 lines after the NSLog that prints out contents from the object I tried to add. – Jacob Johnson Oct 18 '14 at 01:56
  • crewList is nil. This was obvious from the start. – Hot Licks Oct 18 '14 at 02:22
  • All condescension aside, thanks for the help everyone, I am not sure if I misunderstand how the singleton works completely, or if it was ARC that removed the initialized array. – Jacob Johnson Oct 18 '14 at 02:26
  • Singletons are usually misused, one way or the other. Only maybe one case in 5 is their use really justified. – Hot Licks Oct 19 '14 at 18:52

1 Answers1

0

Solved, ok, I knew I was overlooking something...

The first initializing of the jeoData singleton class happens in the initial view controller, however, since nothing is actually added to the crewList mutable array, once the segue to the problematic view controller was made, the crewList array disappeared, rather than be retained like the employeeList.

I called the init function on jeoData.crewList in viewDidLoad and it allows me to add/remove from the array.

The only thing I need to figure out now is if it will still retain the objects in the array after moving to yet ANOTHER view controller.