我收到此錯誤:[NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array
雖然我有75個項目在我的數據庫中。爲什麼它向我顯示這個錯誤?我想從我的數據庫中的項目,但它告訴我,指數爲0。[NSMutableArray objectAtIndex:]:索引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;
}
檢查arrAlert是否爲空? –
check [arrItemsList Count]; – Deepesh