gpt4 book ai didi

objective-c - [NSMutableArray objectAtIndex :]: index 0 beyond bounds for empty array'

转载 作者:太空狗 更新时间:2023-10-30 03:54:11 33 4
gpt4 key购买 nike

我收到此错误:[NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array 尽管我的数据库中有 75 个项目。为什么它向我显示此错误?我试图从我的数据库中获取项目,但它告诉我索引为 0。

+(NSMutableArray *)GetAttitudeItems
{
NSArray *arrDocPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *strDestPath = [NSString stringWithFormat:@"%@/ItemsAndCategory.sqlite",[arrDocPath objectAtIndex:0]];


//sqlite3 *sqlite;

NSMutableArray *arrItemsList;
if(sqlite3_open([strDestPath UTF8String],&database)==SQLITE_OK)
{

const char *strQuery = "SELECT * FROM Items WHERE CategoryName='ATTITUDE' ORDER BY RANDOM()";
NSLog(@"The value is %s",strQuery);
sqlite3_stmt *stmt;
//char *err;

arrItemsList = [[NSMutableArray alloc]init];

if(sqlite3_prepare_v2(database, strQuery , -1,&stmt, NULL)==SQLITE_OK)
{
while (sqlite3_step(stmt)==SQLITE_ROW)
{

NSString *StrCatename = [NSString stringWithUTF8String:(char*)sqlite3_column_text(stmt, 1)];
NSString *StrItemName = [NSString stringWithUTF8String:(char*)sqlite3_column_text(stmt, 0)];

Itemslist *itemlist = [[Itemslist alloc]init];
itemlist.CategoryValue = StrCatename;
itemlist.ItemValue =StrItemName;
[arrItemsList addObject:itemlist];

}

}

if (stmt!=nil)
{
sqlite3_finalize(stmt);
}
}



sqlite3_close(database);

return arrItemsList;

最佳答案

如果您确信您的数据库调用应该返回数据,我将确保您的 if () else () 语句中至少有一个确实被命中。否则,鉴于您在上面对其进行了初始化,您的数组为空:

NSMutableArray *arrAlert = [[NSMutableArray alloc]init];

一个简单的 self 检查方法是在 if else 链的底部添加一个 else 语句,并断言一些东西那里(即至少打印一些东西,对于初学者来说。)

关于objective-c - [NSMutableArray objectAtIndex :]: index 0 beyond bounds for empty array',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11289015/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com