iOS错误:线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x726f635f)?

时间:2014-06-28 13:16:00

标签: ios objective-c multithreading exc-bad-access

我在开发第一个管理数据库的iOS应用程序时遇到了这个问题。 我有一个数据库有游戏的分数,并希望在表中显示它们,当(在运行时)我输入scoreView应用程序崩溃时出现此错误:

Thread 1:EXC_BAD_ACCESS(code=1,address=0x726f635f)

以下是Xcode向我展示负责此问题的代码:

    if (sqlite3_prepare_v2(database, sql , -1, &select_statement, NULL)
    == SQLITE_OK) {
                    while (sqlite3_step(select_statement) == SQLITE_ROW){
                        // read from DB
                        NSString *rank = [NSString stringWithUTF8String:(char *)sqlite3_column_text(select_statement, 0)];
                        NSString *name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(select_statement, 1)];
                        NSString *score = [NSString stringWithUTF8String:(char *)sqlite3_column_text(select_statement, 2)];
                        NSString *date = [NSString stringWithUTF8String:(char *)sqlite3_column_text(select_statement, 3)];

                        // I insert all the string in a dictionary
                        NSDictionary *dictionary = [[NSMutableDictionary alloc] initWithObjectsAndKeys:rank,@"Rank",name,@"Name",score,"Score",date,@"Date",nil];
                        [tempList addObject:dictionary];
                    }

                self.list = tempList;
                sqlite3_finalize(select_statement);
}

错误在于:

NSDictionary *dictionary = [[NSMutableDictionary alloc] initWithObjectsAndKeys:rank,@"Rank",name,@"Name",score,"Score",date,@"Date",nil];

从DB读取后我试图将所有字符串保存在字典中,最后所有字典(在tempList中)都进入“list”,这是一个在表中显示分数的数组。 我在网上看到这个问题可能是由访问同一个对象的不同线程造成的,但我不知道如何解决它。 我正在关注iOS应用程序中使用DB的教程,当然这个问题不存在。 如果有人可以帮助我,我将非常感激,谢谢。

2 个答案:

答案 0 :(得分:1)

您遇到语法错误:

... score, "Score" ...

应为:

... score, @"Score" ...

答案 1 :(得分:0)

首先,在尝试添加对象之前,我没有看到初始化可变数组tempList的行。

NSMutableArray* tempList = [[NSMutableArray alloc]init];

您有语法错误,NSDictionary无法初始化为NSMutableDictionary

NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:rank,@"Rank",name,@"Name",score,@"Score",date,@"Date",nil];
[tempList addObject:dictionary];

对于所有字符串:

NSString *score = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(select_statement, 2)]