Getting an uncaught exception while iterating through result set in iOS sqlite DB

89 views Asked by At

following snippet gives me :

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '- [__NSCFString count]: unrecognized selector sent to instance 0x8ce0c00'

-(void)loadInfo{
    // Create the query.
    NSString *query = [NSString stringWithFormat:@"select * from tableName where      category=\"%@\" ", @"cat1"];

    // Load the relevant data.
    NSArray *results = [[NSArray alloc] initWithArray:[self.dbManager loadDataFromDB:query]];
    NSMutableArray *List = [[NSMutableArray alloc]init];

    NSLog(@"Count..:%d",results.count); 

    for(int i=0; i<results.count; i++){
        List = [[results objectAtIndex:i] objectAtIndex:[self.dbManager.arrColumnNames indexOfObject:@"itemName"]];
    }
    NSLog(@"Result..:::: %d", [List count]);
}

What is going wrong here? Cant i print List count? dbmanager is what I have implemented using eg. given in - http://www.appcoda.com/sqlite-database-ios-app-tutorial/

1

There are 1 answers

0
Sergey Kalinichenko On BEST ANSWER

This operation replaces List on every loop iteration with a different object:

List = [[results objectAtIndex:i] objectAtIndex:[self.dbManager.arrColumnNames indexOfObject:@"itemName"]];

Judging from the error, the object happens to be NSString.

What you probably wanted is to add objects to your mutable array List:

[List addObject:[[results objectAtIndex:i] objectAtIndex:[self.dbManager.arrColumnNames indexOfObject:@"itemName"]]];