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);
I don't understand why this NSInteger counter increments to exactly 4 times the true value of database rows. Maybe this is stupid but I really just don't get it...
Thanks so far :)
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为 i 是一个指针,并且您正在递增指针值,该值很可能以 4 为步长(NSInteger 指针的大小)。只要删除指针 * 引用就可以了。
理论上你可以用困难的方式做到这一点。
从:
基础数据类型参考
Because i is a pointer and you are incrementing the pointer value which will most likely be in steps of 4 (size of NSInteger pointer). Just remove the pointer * reference and you should be good.
In theory you COULD do this the hard way.
From:
Foundation Data Types Reference
i
未声明为NSInteger
,它被声明为指向NSInteger
的指针。由于
NSInteger
为 4 个字节,因此当您加 1 时,指针实际上会增加 1NSInteger
的大小,即 4 个字节。之所以会出现这种混乱,是因为 NSInteger 不是对象,因此您不需要声明指向它的指针。将您的声明更改为预期行为:
i
is not declared as anNSInteger
, it's declared as a pointer to anNSInteger
.Since an
NSInteger
is 4 bytes, when you add 1, the pointer actually increases by the size of 1NSInteger
, or 4 bytes.This confusion is arising because
NSInteger
is not an object, so you don't need to declare a pointer to it. Change your declaration to this for the expected behaviour: