当我从数据库中获取nil数据时app崩溃

时间:2012-12-01 08:47:44

标签: iphone objective-c ios

我从数据库中获取数据时的代码: -

while(sqlite3_step(dataRows) == SQLITE_ROW)
        {
            clsGlobal *temp=[[clsGlobal alloc]init];
            //temp.CatId=sqlite3_column_int(dataRows,0);
            temp.QId =sqlite3_column_int(dataRows,0);
            temp.QName= [NSString stringWithUTF8String:(char *)sqlite3_column_text(dataRows,1)];
            temp.ImagePath= [NSString stringWithUTF8String:(char *)sqlite3_column_text(dataRows,2)]; //app crash when imagepath is nil

            NSLog(@"%d%@%@",temp.QId,temp.QName,temp.ImagePath);

所以请给我正确的建议和有用的链接。

1 个答案:

答案 0 :(得分:3)

在尝试将指针变为NSString

之前检查指针
char *text = (char *)sqlite3_column_text(dataRows,2);
temp.ImagePath = text ? [NSString stringWithUTF8String:text] : @"no image path in database";