0

リテラル整数で NSArray データにアクセスできません。いくつかの例外、インデックス バウド (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 に答える