Ok this is third day of working with Xcode so please be nice.
I am trying to set up a simple table to display records from an existing sqlite database. I’ve got as far as loading the database into a NSMutableArray and have verified the size is correct, but I can’t loop through it and get the data properly out. All that displays is the last record of the table.
Here is the sample code:
while([results next]) {
self.subject_id = [results stringForColumn:@"SUBJECT_ID"];
self.subject = [results stringForColumn:@"SUBJECT"];
self.category = [results stringForColumn:@"CATEGORY"];
[subjects addObject:self];
NSLog(@"AFTER record: %@ - %@ - %@", self.subject_id, self.subject, self.category);
}
As the loop prints out to the log I verify that the correct data is being looped through.
Then I run this immediately after populating the array and I get the same last entry, each time:
//PULL UP THE THINGY AND SEE IF IT WORKS
Subject *sub;
int x = 0;
for (sub in subjects){
Subject *xsub = [subjects objectAtIndex:x];
NSLog(@" %i sub data ------- %@, ----- %@", x, xsub.subject, xsub.category);
x = x+1;
};
sub = [subjects objectAtIndex:50];
NSLog(@" sub data ------- %@, ----- %@", sub.subject, sub.category);
I even tried specifically pulling up a record from the Array like this but I get the same last record instead of the record I asked for:
sub = [subjects objectAtIndex:50];
“Subject” is a class giving the field names of my table. I haven’t used the entity feature yet.
My header file declares the field names like this:
@property (nonatomic, retain) NSString * subject_id;
@property (nonatomic, retain) NSString * subject;
@property (nonatomic, retain) NSString * category;
I used the FMDB wrapper to get the data from my database and pieced together the classes from a couple different tableView examples such as the Animals one.
Can anyone give me some direction on how to debug this?
My guess would be either that each array object has had the same text loaded into it, or that the same object has been added to the array multiple times.
Either way, I’d take a long hard look at the array loading code.