iOS如何修复无法返回对象索引的数组

问题描述 投票:-3回答:2

我的数组为我要查找的对象的索引返回了错误的数字。我的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 ...

ios arrays ios7 nsarray
2个回答
1
投票

正如我在评论中所述,您已经在chamberCategoryArray中填充了placesChamber对象的实例。


0
投票

我发现您在以下3行代码中使用了indexOfObject:

© www.soinside.com 2019 - 2024. All rights reserved.