2011-03-22 63 views
5

我不明白爲什麼這個NSInteger計數器的增量正好是數據庫行的真實值的4倍。也許這是愚蠢的,但我真的不明白這一點...NSInteger計數4次?

感謝迄今:)

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); 

回答

11

因爲我是一個指針,你是遞增的指針值,這將最有可能在4(NSInteger的指針的大小)的步驟。只要刪除指針*參考,你應該很好。

NSInteger i = 0; 

for (NSDictionary *teil in gText) { 

從理論上講,你可以這麼做。

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

來源: Foundation Data Types Reference

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

啊,所以NSInteger不是像所有其他NSS一樣的常規對象...,這是一種原始類型? – LaK 2011-03-22 23:05:35

+0

是的,其實很有趣,因爲它是long或int的typedef。我將包含文檔中的剪輯。 – Suroot 2011-03-22 23:06:59

+0

好吧,似乎讓64位更直觀......對我來說很好,謝謝:) – LaK 2011-03-22 23:27:04

1

i沒有聲明爲NSInteger,它的聲明爲一個指向NSInteger

由於NSInteger是4個字節,因此當您加1時,指針實際上增加了1 NSInteger或4個字節的大小。

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

這種混亂是因爲所產生的NSInteger不是一個對象,所以你並不需要一個指針聲明它。更改聲明此爲預期的行爲:

NSInteger i = 0; 
+0

「我不聲明爲一個NSInteger,它的聲明爲一個NSInteger。」呃...笏? – JustSid 2011-03-22 23:04:09

+0

@JustSid:哦哇...是的,那不是我的意思... = P – 2011-03-22 23:08:44