My array is returning an incorrect number for an index of an object that I am looking for. My array.count tells me there are 248 objects in the array, but the index that is returned is 2147483647. I saw this post that is similar to mine issue. I used the equality testing that was proposed in this post, but fell short of figuring out why this was happening and how to fix it.
Here is my code: (Edited with solution)
-(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
}

Here is my placesChamber.m: (Needed for solution)
#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
NSNotFound. That means your object isn't in the array.!=NSNotFoundthen make what you want with it , else it is not found in your array.placesChamberobjects but you are looking for anNSNumberobject and anNSStringliteral. That will never work.po.