未识别的选择器发送给实例

3

我正在创建一个样例应用程序,其中我正在读取数据库并将用户图像和名称显示到表视图中。但是我遇到了以下异常:

[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];

以上代码出现了异常,但我无法找出问题。:(


2个回答

8

您的appDelegate变量指向UIApplication而不是其delegate属性。更改此赋值...

AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication];

to...

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

1

修改以下行:

AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication];

AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接