我在开发第一个管理数据库的 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];
从数据库读取后,我尝试将所有字符串保存在字典中,最后所有字典(在 tempList 中)都进入“列表”,这是一个在表中显示分数的数组。 我在网上看到这个问题可能是由不同的线程访问同一对象引起的,但我不知道如何解决。 我正在遵循在 iOS 应用程序中使用 DB 的教程,当然这个问题不存在。 如果有人可以帮助我,我将非常感激,谢谢。
最佳答案
您有一个语法错误:
... score, "Score" ...
应阅读:
... score, @"Score" ...
关于iOS错误: Thread 1:EXC_BAD_ACCESS(code=1,地址=0x726f635f)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24467198/