views:

20

answers:

1

I am loading json into a list of objects I am adding to a NSMutableArray which seems to be working fine except for the fact that I can't seem to keep the array around to access later.

This is my .h:

@interface ClientController : UITableViewController {
    NSMutableData *responseData;
    NSMutableArray *ClientList;
}

@property (nonatomic, retain) NSMutableArray *ClientList;

In my .m I add:

@synthesize ClientList;

later I have a method that populates it (Client is a class I have as well):

/* some code that parses the Json */
for (int i = 0; i < [items count]; i++) 
        {
            Client* client = [[Client alloc] init];
            client.CompanyName = [[items objectAtIndex:i] objectForKey:@"CompanyName"]; 
            client.ClientID = [[items objectAtIndex:i] objectForKey:@"ClientID"];           

            [ClientList addObject:client];

            NSLog(@"CompanyName: %@\n", client.CompanyName);
            [client release];
        }

Console shows me that is is working properly because I see the data I expect BUT when I do this I get a null value:

NSLog(@"Clients Count: %@\n", [ClientList count]);

I need to loop though this to build my table - so what am I missing here?

+1  A: 

Gotta initialize your ClientList, thusly:

-(void)viewDidLoad
{
  self.ClientList = [NSMutableArray array];
}

Otherwise self.ClientList is a nil pointer, and as a result, [self.ClientList addObject:] is a silent no-op, as is [self.ClientList count].

By the way, the name "clientList" would probably be preferable. We like our variables to start lower-cased in Obj-C.

Dan Ray
I believe that did the trick but now I get a 'EXC_BAD_ACCESS' error when I get to this line: NSLog(@"Clients Count: %@\n", [clientList count]);
Slee
doing some more reading and it seems since I release my client once I add it, it is no longer available in the array - thanks for the help!
Slee
@Max Fraser: `[clientList count]` returns an int. The NSString format token for that is %d, not %@. That's why the EXEC_BAD_ACCESS. I know it seems like something that would give you a more polite error, but there it is.
Dan Ray
@Max: Also, the array will retain the client when it adds it. Releasing it after adding it is the right thing to do.
Dan Ray