0

我无法使用文字整数访问 NSArray 数据,它会引发一些异常,索引 bouds (0 , 0) 但实际上这是错误的,因为如果我动态循环它,它就可以工作......我的代码:

        for (NSString *cada in barcos)
        {
            NSArray *datos = [cada componentsSeparatedByString:@";"];
            NSString *consulta = [NSString stringWithFormat:@"INSERT OR REPLACE INTO Barco (ID,Zona,Nombre,Matricula,Imagen) VALUES ('%d','%@',\"%@\",\"\",\"%@\");",contador,[datos objectAtIndex:0],[datos objectAtIndex:1],[datos objectAtIndex:2]];
            insert_stmt = (char *)[consulta UTF8String];
            sqlite3_prepare_v2(db, insert_stmt, -1, &statement, NULL);
            sqlite3_finalize(statement);
            contador++;
        }

我对此进行了测试并且可以工作......但我不希望这样:

        for (NSString *cada in barcos)
        {
            NSArray *datos = [cada componentsSeparatedByString:@";"];
            for (int c = 0; c < [datos count]; c++)
                NSLog(@"%@",[datos objectAtIndex:c]);
            // It prints all very good, there is only 0 to 2 position always
        }

编辑:

        for (NSString *cada in barcos)
        {
            NSArray *datos = [cada componentsSeparatedByString:@";"];
            for (int c = 0; c < [datos count]; c++)
            {
                NSLog(@"posicion array %d - dato: %@",c,[datos objectAtIndex:c]);
            }// Here we are good, the loop is OK
            NSString *consulta = [NSString stringWithFormat:@"INSERT OR REPLACE INTO Barco (ID,Zona,Nombre,Matricula,Imagen) VALUES ('%d','%@',\"%@\",\"\",\"%@\");",contador,[datos objectAtIndex:0],[datos objectAtIndex:1],[datos objectAtIndex:2]]; // Here crash, cannot go to objectAtIndex:1
            /*insert_stmt = (char *)[consulta UTF8String];
            sqlite3_prepare_v2(db, insert_stmt, -1, &statement, NULL);
            sqlite3_finalize(statement);*/
            contador++;
        }

我的数组打印了这个:

2013-01-22 12:54:36.386 whales[4076:707] posicion array 0 - dato: Spirit of the seas
2013-01-22 12:54:36.403 whales[4076:707] posicion array 1 - dato: spirit_of_the_seas.bmp
2013-01-22 12:54:36.410 whales[4076:707] posicion array 2 - dato: 1
2013-01-22 12:54:36.419 whales[4076:707] posicion array 0 - dato: Nashira I
2013-01-22 12:54:36.425 whales[4076:707] posicion array 1 - dato: nashira1.bmp
2013-01-22 12:54:36.432 whales[4076:707] posicion array 2 - dato: 2
2013-01-22 12:54:36.441 whales[4076:707] posicion array 0 - dato: Tina Excursiones
2013-01-22 12:54:36.448 whales[4076:707] posicion array 1 - dato: tinaexcursiones.bmp
2013-01-22 12:54:36.454 whales[4076:707] posicion array 2 - dato: 3
2013-01-22 12:54:36.461 whales[4076:707] posicion array 0 - dato: Catlanza
2013-01-22 12:54:36.467 whales[4076:707] posicion array 1 - dato: catlanza.bmp
2013-01-22 12:54:36.473 whales[4076:707] posicion array 2 - dato: 3
2013-01-22 12:54:36.478 whales[4076:707] posicion array 0 - dato: 

那是我的 for + NSLog 所以没关系,但手动不行

4

2 回答 2

1

使用 if 保护对数组的访问:

if( datos.count>=3)
{
    NSString *consulta = [NSString stringWithFormat:@"INSERT OR REPLACE INTO Barco (ID,Zona,Nombre,Matricula,Imagen) VALUES ('%d','%@',\"%@\",\"\",\"%@\");",contador,[datos objectAtIndex:0],[datos objectAtIndex:1],[datos objectAtIndex:2]];
}
else
{
    NSLog(@"datos count is %u",datos.count);
}

这将使您了解 datos 数组何时没有 3 个元素。

于 2013-01-22T12:54:25.613 回答
0

我的解决方法是:

        for(int c = 0; c < ([barcos count] - 1); c++)
        {
            NSArray *datos = [[barcos objectAtIndex:c] componentsSeparatedByString:@";"];
            NSString *consulta = [NSString stringWithFormat:@"INSERT OR REPLACE INTO Barco (ID,Zona,Nombre,Matricula,Imagen) VALUES ('%d','%@',\"%@\",\"\",\"%@\");",contador,[datos objectAtIndex:0],[datos objectAtIndex:1],[datos objectAtIndex:2]];
            insert_stmt = (char *)[consulta UTF8String];
            sqlite3_prepare_v2(db, insert_stmt, -1, &statement, NULL);
            sqlite3_finalize(statement);
            contador++;
        }

如此简单的解决方案...

于 2013-01-22T13:16:14.627 回答