开发者

Objective C: How does 7 - 1 = 3?

开发者 https://www.devze.com 2023-02-16 07:10 出处:网络
NSLog(@\"Before: %d\",currentArticle); currentArticle--; NSLog(@\"SUBTRACT %d\",currentArticle); \"currentArticle\" is an integer. This is only being echoed once in my console. If I do not run this
NSLog(@"Before: %d",currentArticle);
currentArticle--;
NSLog(@"SUBTRACT %d",currentArticle);

"currentArticle" is an integer. This is only being echoed once in my console. If I do not run this subtraction, the number "currentArticle" remains at 7.

This is being run in the main thread, and only run once per user interaction.

I have also tried

currentArticle = currentArticle - 1;

With the same result. Am I taking crazy pills?

Thanks!

Edit:

Declared as follows:

extern int *currentArticle;

And assigned later as:

currentArticle = 0;

I tried rewriting as this:

int *curArticle; // in my .h file

curArticle = 1;

And then I run the

curArticle--;

and it still开发者_开发知识库 decrements by two...

I have stepped through the code and ensured there are no other calls hitting this variable.. Thanks for the feedback so far, I will keep hacking away at it.


I concur with the comments above. I'd bet a dollar that your code looks like:

int *currentArticle = 7; // or something

currentArticle may not even be a pointer to an int, specifically, but it's very likely a pointer to some 4-byte type. The '--' and '++' operators, when applied to pointers, decrement or increment by the size of the type that's pointed to.


Things I think of: Threads (if it's a strange problem, there are threads)? Or is it called by an event (which is triggered more than once)?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号