在2次运行后(数据库中只有2个值),我得到2个不同的值,就像它应该的那样。然后我把它写入NSMutableArray。
但是只有第二个值两次。它不应该添加到数组的末尾吗?我做错了什么?
- (NSMutableArray *)getItemsFromDatabaseWithName:(NSString *)databaseName fromTable:(NSString *)tableName andConstraint:(NSString *)constraint
{
NSString *absolutePath = [[NSBundle mainBundle].resourcePath stringByAppendingPathComponent:databaseName];
NSLog(@"%@", absolutePath);
//Datenbank öffnen --- "**" bedeuten "&" verwenden
sqlite3_open([absolutePath UTF8String], &_database);
//check if there is a constraint and if not take 2nd statement
if (![constraint isEqualToString:@""])
{
_statement = [NSString stringWithFormat:@"select * from %@ where %@",tableName, constraint];
}
else
{
_statement = [NSString stringWithFormat:@"select * from %@",tableName];
}
const char *charStatement = [_statement cStringUsingEncoding:NSUTF8StringEncoding];
sqlite3_stmt *results;
//new array to return values
_mutableItemArray = [NSMutableArray new];
//new ItemModel
ItemModel *tmpItem = [ItemModel new];
if (sqlite3_prepare_v2(_database, charStatement, -1, &results, NULL)== SQLITE_OK)
{
while (sqlite3_step(results) == SQLITE_ROW)
{
_charItemName = (char *)sqlite3_column_text(results, 1);
[tmpItem setItemName:[NSString stringWithUTF8String:_charItemName]];
_charItemDescription = (char *)sqlite3_column_text(results, 2);
[tmpItem setItemDescription:[NSString stringWithUTF8String:_charItemDescription]];
_charItemYear = (char *)sqlite3_column_text(results, 3);
[tmpItem setItemYear:[_dateFormat dateFromString:[NSString stringWithUTF8String:_charItemYear]]];
_charItemRecommendedBy = (char *)sqlite3_column_text(results, 4);
[tmpItem setItemRecommendedBy:[NSString stringWithUTF8String:_charItemRecommendedBy]];
_charItemImage = (char *)sqlite3_column_text(results, 5);
[tmpItem setItemImage:[NSString stringWithUTF8String:_charItemImage]];
[_mutableItemArray addObject:tmpItem];
#warning here I get the 2 items correct
NSLog(@"ItemName: %@",[tmpItem getItemName]);
NSLog(@"ItemName: %@",[tmpItem getItemDescription]);
}
}
sqlite3_close(_database);
#warning here I get 2 times the same item ???
NSLog(@"ItemName: %@",[_mutableItemArray objectAtIndex:0]);
NSLog(@"ItemName: %@",[_mutableItemArray objectAtIndex:1]);
return _mutableItemArray;
}
答案 0 :(得分:0)
您只需创建一个对象tmpItem
。
这将被添加到数组中,并且在下一次运行while循环时,您不是要创建新的tmpItem
,而是修改旧的tmpItem
并将其添加到数组中。
因此,您将得到一个包含指向同一对象tmpItem
的两个指针的数组(具有最新状态)。
解决方案:在while循环中创建{{1}}。
答案 1 :(得分:0)
如果你查看代码,你会在while循环中看到你一次又一次地设置同一个对象(tmpItem
),这就是为什么你的数组最后更新了同一个对象的值。
现在看一下你在while循环中会注意到的代码,我们正在创建新对象并将其存储在NSArray
中。
- (NSMutableArray *)getItemsFromDatabaseWithName:(NSString *)databaseName fromTable:(NSString *)tableName andConstraint:(NSString *)constraint
{
NSString *absolutePath = [[NSBundle mainBundle].resourcePath stringByAppendingPathComponent:databaseName];
NSLog(@"%@", absolutePath);
//Datenbank öffnen --- "**" bedeuten "&" verwenden
sqlite3_open([absolutePath UTF8String], &_database);
//check if there is a constraint and if not take 2nd statement
if (![constraint isEqualToString:@""])
{
_statement = [NSString stringWithFormat:@"select * from %@ where %@",tableName, constraint];
}
else
{
_statement = [NSString stringWithFormat:@"select * from %@",tableName];
}
const char *charStatement = [_statement cStringUsingEncoding:NSUTF8StringEncoding];
sqlite3_stmt *results;
//new array to return values
_mutableItemArray = [NSMutableArray new];
//new ItemModel
if (sqlite3_prepare_v2(_database, charStatement, -1, &results, NULL)== SQLITE_OK)
{
while (sqlite3_step(results) == SQLITE_ROW)
{
ItemModel *tmpItem = [ItemModel new];
_charItemName = (char *)sqlite3_column_text(results, 1);
[tmpItem setItemName:[NSString stringWithUTF8String:_charItemName]];
_charItemDescription = (char *)sqlite3_column_text(results, 2);
[tmpItem setItemDescription:[NSString stringWithUTF8String:_charItemDescription]];
_charItemYear = (char *)sqlite3_column_text(results, 3);
[tmpItem setItemYear:[_dateFormat dateFromString:[NSString stringWithUTF8String:_charItemYear]]];
_charItemRecommendedBy = (char *)sqlite3_column_text(results, 4);
[tmpItem setItemRecommendedBy:[NSString stringWithUTF8String:_charItemRecommendedBy]];
_charItemImage = (char *)sqlite3_column_text(results, 5);
[tmpItem setItemImage:[NSString stringWithUTF8String:_charItemImage]];
[_mutableItemArray addObject:tmpItem];
NSLog(@"ItemName: %@",[tmpItem getItemName]);
NSLog(@"ItemName: %@",[tmpItem getItemDescription]);
}
}
sqlite3_close(_database);
NSLog(@"ItemName: %@",[_mutableItemArray objectAtIndex:0]);
NSLog(@"ItemName: %@",[_mutableItemArray objectAtIndex:1]);
return _mutableItemArray;
}