我的数组为我要查找的对象的索引返回了错误的数字。我的array.count告诉我数组中有248个对象,但返回的索引是2147483647。我看到了类似于我的问题的post。我使用了本文中提出的相等性测试,但是没有弄清楚为什么会发生这种情况以及如何解决它。
这是我的代码:(使用解决方案编辑)
-(void)getChamberPrimaryCategories
{
NSString *docsDir;
NSArray *dirPaths;
chamberCategoryArray = [[NSMutableArray alloc] init];
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];
// Build the path to the database file
_databasePath = [[NSString alloc]
initWithString: [docsDir stringByAppendingPathComponent:
@"the_app.db"]];
NSLog(@"%@",_databasePath);
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: _databasePath ] == NO)
{
const char *dbpath = [_databasePath UTF8String];
if (sqlite3_open(dbpath, &_the_app_database) == SQLITE_OK)
{
//do nothing
} else {
NSLog(@"Failed to open database");
}
}
const char *dbpath = [_databasePath UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &_the_app_database) == SQLITE_OK)
{
NSString *querySQL;
querySQL = @"SELECT DISTINCT CHAMBER_PRIMARY_CATEGORY FROM PLACES WHERE CHAMBER_PRIMARY_CATEGORY IS NOT '' AND CHAMBER_PRIMARY_CATEGORY IS NOT NULL ORDER BY CHAMBER_PRIMARY_CATEGORY ASC";
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(_the_app_database,
query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
NSLog(@"Query the database now.");
// if (sqlite3_step(statement) != SQLITE_ROW) {
// NSLog(@"Not okay");
// }
while (sqlite3_step(statement) == SQLITE_ROW) {
NSLog(@"Getting Information:");
placesChamber *chamberCategoryObject = [[placesChamber alloc]init];
NSString *placesPrimaryChamberCategory = [[NSString alloc]
initWithUTF8String:
(const char *) sqlite3_column_text(
statement, 0)];
chamberCategoryObject.primary_category = placesPrimaryChamberCategory;
NSLog(@"%@",placesPrimaryChamberCategory);
[chamberCategoryArray addObject:chamberCategoryObject];
}//end while
}//end if
else
{
NSLog(@"There is nothing in this database!");
NSLog(@"%s",sqlite3_errmsg(_the_kearney_app_database));
}
sqlite3_finalize(statement);
sqlite3_close(_the_kearney_app_database);
[chamberCategoryTableView reloadData];
NSLog(@"content offset: %f",chamberCategoryTableView.contentOffset.y);
if (chamberCategoryTableView.contentOffset.y == 0)
{
placesChamber *searchChamber = [[placesChamber alloc] init];
searchChamber.primary_category = @"Women's Clothing";
NSUInteger index = [chamberCategoryArray indexOfObject:searchChamber];
if (index == NSNotFound) {
// no such chamber category
NSLog(@"no such thing found");
} else {
// Found it at index
NSLog(@"found it!");
}
}//end if
}//end if
}
“ >>
这是我的位置Chamber.m :(需要解决方案)
#import "placesChamber.h"
@implementation placesChamber
@synthesize ID;
@synthesize name;
@synthesize type;
@synthesize category;
@synthesize latitude;
@synthesize longitude;
@synthesize primary_category;
@synthesize secondary_category;
- (BOOL)isEqual:(id)object
{
if ( self == object ) {
return YES;
}
if ( ![object isKindOfClass:[placesChamber class]] ) {
return NO;
}
if ( ![primary_category isEqualToString:[object primary_category]] ) {
return NO;
}
return YES;
}
@end
我的数组为我要查找的对象的索引返回了错误的数字。我的array.count告诉我数组中有248个对象,但是返回的索引是2147483647。I ...
正如我在评论中所述,您已经在chamberCategoryArray
中填充了placesChamber
对象的实例。
我发现您在以下3行代码中使用了indexOfObject:
: