我正在创建一个示例应用程序,其中我正在读取数据库并将用户图像和名称显示在tableview中。 但我得到了以下异常
[UIApplication userListMutableArray]: unrecognized selector sent to instance 0x6816330
以下是我的代码段
AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication];
if(sqlite3_open([dbPath UTF8String], &database)==SQLITE_OK)
{
const char *sql = "select NAME,IMAGE from user";
sqlite3_stmt *selectstmt;
if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL)==SQLITE_OK)
{
while (sqlite3_step(selectstmt) == SQLITE_ROW) {
UserData *userData = [[UserData alloc] init];
userData.userName = [NSString stringWithUTF8String:(char*)sqlite3_column_text(selectstmt, 0)];
NSData *data = [[NSData alloc] initWithBytes:sqlite3_column_blob(selectstmt, 1) length:sqlite3_column_bytes(selectstmt, 1)];
if(data == nil)
NSLog(@"image not found");
userData.userImage = [UIImage imageWithData:data];
**[appDelegate.userListMutableArray addObject:userData];**
}
}
}
else
{
sqlite3_close(database);
}
[appDelegate.userListMutableArray addObject:userData];
上面的行是异常,但我无法追踪问题。 :(
答案 0 :(得分:8)
您的appDelegate变量指向UIApplication而不是其delegate属性。更改此作业......
AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication];
为...
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
答案 1 :(得分:1)
更改以下行
AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication];
到
AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];