在iOS sqlite DB中迭代结果集时获取未捕获的异常

时间:2014-11-13 05:05:35

标签: ios sqlite uncaught-exception

以下代码段给了我:

  

由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [__ NSCFString count]:无法识别的选择器发送到实例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]);
}

这里出了什么问题?我打印列表计数? dbmanager是我用eg实现的。在 - http://www.appcoda.com/sqlite-database-ios-app-tutorial/

中给出

1 个答案:

答案 0 :(得分:0)

此操作在每个循环迭代中使用不同的对象替换List

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

从错误判断,对象恰好是NSString

您可能想要的是将对象添加到可变数组List

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