unrecognized selector sent to instance

2019-07-04 02:23发布

问题:

I am creating one sample application in which i am reading database and displaying user image and name into tableview. but i am getting following exception

[UIApplication userListMutableArray]: unrecognized selector sent to instance 0x6816330

following is my code snippet

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

above line is giving exception but i am not able to track problem. :(

回答1:

Your appDelegate variable is pointing to the UIApplication and not its delegate property. Change this assignment...

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

to...

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


回答2:

Change the following line

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

to

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