我的应用程序中有一个共享的 nsmutablearray。我正在从 sqlite 数据库中提取一堆数据并添加到数组中。该数组具有正确数量的对象作为数据库,但是当我调用 objectAtIndex 时,我总是得到相同的值 - 最后一个记录值。
所以我不确定我的应用程序是否无法添加到共享数组或从中提取数据。这是一些代码:最后两个 NSLog 给了我相同的输出,这是不正确的
SYSAppDelegate *appDelegate = (SYSAppDelegate *)[[UIApplication sharedApplication] delegate];
Contra *contra = [[Contra alloc]init];
sqlite3_stmt *statement;
int recordCount = 0;
if (sqlite3_open([appDelegate.dbPathString UTF8String], &_DB)==SQLITE_OK) {
[appDelegate.array removeAllObjects];
NSString *querySql = [NSString stringWithFormat:@"SELECT * FROM DATATABBLE"];
const char* query_sql = [querySql UTF8String];
if (sqlite3_prepare(_DB, query_sql, -1, &statement, NULL)==SQLITE_OK) {
while (sqlite3_step(statement)==SQLITE_ROW) {
NSString *start = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 1)];
NSString *stop = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 2)];
NSString *cTime = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 3)];
NSString *intensity = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 4)];
NSString *timeElapsed = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 5)];
NSString *hour = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 6)];
[contra setStartDate:start];
[contra setStopDate:stop];
[contra setIntensity:[intensity intValue]];
[contra setCTime:[cTime intValue]];
[contra setTimeElapsed:[timeElapsed intValue]];
[contra setHour:[hour intValue]];
[appDelegate.array addObject:contra];
recordCount++;
}
}
}
//Get data from array
//Get last object
contra = [appDelegate.array lastObject];
NSLog(@"cont length: %i.", contra.cTime);
//Get an earlier object
contra = [appDelegate.array objectAtIndex:recordCount -2];
NSLog(@"two cont length: %i", contra.cTime);