NSMutableArray不会填满

时间:2012-01-08 23:37:51

标签: objective-c ios cocoa-touch

我正在尝试使用sqlite数据库中的数据填充NSmutableAray。

ItemShow类:

@interface ItemShow : NSObject 
{
NSInteger itemID;
NSString *itemYear;
NSInteger rarity;
NSString *mintage;
NSInteger availability;
NSInteger quality;
NSString *mintMark;
NSString *masterMark;
NSString *dateCode;
NSString *dateDescription;
}

Getters / setter没问题

DBAccess类中的方法:

-(NSMutableArray*)getItemsOverView:(int)itemID
{
NSMutableArray *itemsArray=[[[NSMutableArray alloc]init]autorelease];
const char *sqlItems=sqlite3_mprintf("SELECT itm.itemID,itm.itemYear,itm.rarity,itm.mintage,iaval.availability as avalibility,iaval.quality as quality,itm.Mintmark,itm.masterMark,dc.dateCode,dc.dateDescription\
                                     from items as itm\
                                     join itemAvailability as iaval on iaval.itemID=itm.itemID\
                                     join dateCultures as dc ON dc.dateCultureID=itm.dateCulture\
                                     WHERE itm.itemID=%i",itemID);
sqlite3_stmt *statement;
int sqlResult = sqlite3_prepare_v2(database, sqlItems, -1, &statement, NULL);
if ( sqlResult== SQLITE_OK)
{
    while (sqlite3_step(statement) == SQLITE_ROW)
    {
        ItemShow *itemShow=[[ItemShow alloc]init];
        itemShow.itemID=sqlite3_column_int(statement, 0);
        char *itemYear=(char *)sqlite3_column_text(statement, 1);
        itemShow.rarity=sqlite3_column_int(statement, 2);
        char *mintage=(char *)sqlite3_column_text(statement, 3);
        itemShow.availability=sqlite3_column_int(statement, 4);
        itemShow.quality=sqlite3_column_int(statement, 5);
        char *mintMark=(char *)sqlite3_column_text(statement, 6);
        char *masterMark=(char *)sqlite3_column_text(statement, 7);
        char *dateCode=(char *)sqlite3_column_text(statement, 8);
        char *dateDescription=(char *)sqlite3_column_text(statement, 9);

        itemShow.itemYear=(itemYear)?[NSString stringWithUTF8String:itemYear]:@"";
        itemShow.mintMark=(mintMark)?[NSString stringWithUTF8String:mintMark]:@"";
        itemShow.masterMark=(masterMark)?[NSString stringWithUTF8String:masterMark]:@"";
        itemShow.mintage=(mintage)?[NSString stringWithUTF8String:mintage]:@"Unknown";
        itemShow.dateCode=(dateCode)?[NSString stringWithUTF8String:dateCode]:@"";
        itemShow.dateCode=(dateDescription)?[NSString stringWithUTF8String:dateDescription]:@"";

        [itemsArray addObject:itemShow];
        [itemShow release];

    }
    sqlite3_finalize(statement);
}
else
{
    [self dbConnectionError];
}

return itemsArray;
}
ViewDidLoad中的

代码

DBAccess *dbAccess=[[DBAccess alloc]init];
self.dataArr=[dbAccess getItemsOverView:itemID];
   // ItemShow *itmShow=[dataArr objectAtIndex:1];
NSLog(@"%d",[dataArr count]); //returns 1. So, empty array
[dbAccess closeDataBase];
[dbAccess release];

在其他视图中,相同的构造工作正常,但现在它给出了错误 - 空数组超出范围。

检查了什么

  1. 选择作品。在SQLiteStudio中检查它
  2. itemID是int,并且视图从上一个视图中获取正确,就像它获得标题一样。

1 个答案:

答案 0 :(得分:-3)

我自己解决了。刚刚制定了新方法。现在它可以工作