Domanda

Non capisco perché questo contatore Nsinteger incrementi esattamente 4 volte il vero valore delle righe del database. Forse questo è stupido ma non lo capisco davvero ...

Grazie finora :)

NSInteger *i;
i = 0;

for ( NSDictionary *teil in gText ) {

    //NSLog(@"%@", [teil valueForKey:@"Inhalt"]);

    [databaseWrapper addEntry:[teil valueForKey:@"Inhalt"] withTyp:[teil valueForKey:@"Typ"] withParagraph:[teil valueForKey:@"Paragraph"]];

    i+=1;
}

NSLog(@"Number of rows created: %d", i);
È stato utile?

Soluzione

Perché sono un puntatore e stai aumentando il valore del puntatore che molto probabilmente sarà in passaggi di 4 (dimensioni del puntatore Nsinteger). Rimuovere il riferimento del puntatore * e dovresti essere buono.

NSInteger i = 0;

for ( NSDictionary *teil in gText ) {

In teoria potresti farlo nel modo più duro.

NSInteger *i;
*i = 0;
for ( NSDictionary *teil in gText ) {
...
*i = *i + 1;
...

Da:Riferimento dei tipi di dati della fondazione

#if __LP64__ || TARGET_OS_EMBEDDED || TARGET_OS_IPHONE || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64
typedef long NSInteger;
#else
typedef int NSInteger;
#endif

Altri suggerimenti

i non è dichiarato come un NSInteger, è dichiarato un puntatore a un NSInteger.

Da un NSInteger è 4 byte, quando aggiungi 1, il puntatore aumenta effettivamente della dimensione di 1 NSInteger, o 4 byte.

i = 0;
...
i += 1; //Actually adds 4, since sizeof(NSInteger) == 4
...
NSLog(@"%d", i); //Prints 4

Questa confusione è sorgente perché NSInteger non è un oggetto, quindi non è necessario dichiarare un puntatore ad esso. Cambia la tua dichiarazione a questo per il comportamento previsto:

NSInteger i = 0;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top