Objective-C:int值无变化 [英] Objective-C: int value changing without cause
问题描述
Objective-C:我需要帮助保留int的值。没有我的命令,它正在改变我。
Objective-C: I need help retaining the value of an int. It's changing on me without my command.
最初的问题是:你如何声明并保留一个int?,这在另一篇文章中得到满足: Objective-C:你如何声明并保留一个int?
The original question was: "How do you declare and retain an int?", that was satisfied in another post here: Objective-C: How do you declare and retain an int?
现在我有一个问题,即18的int变为2,不知何故单独。
Now I have a problem where an int that was 18 is changing to 2, somehow on its own.
这是我的代码:
@interface Game : Layer // this is from cocos2d
{
int maxSprites;
}
@implementation Game
-(void)initVariables
{
maxSprites = 18;
}
稍后,当我将其打印出来时,例如
Later on, when I print it out, like
NSLog(@" maxSprites = %d ", maxSprites);
我得到:
maxSprites = 2
要求它为18,崩溃或不要做的操作不行,好像现在只有2个。
And operations that require it to be 18, crash or don't work, as if it's really just 2 now.
这怎么可能? =)
Apple + Shift + F 显示没有其他用法的 maxSprites
变量。
Apple + Shift + F reveals no other usage of the maxSprites
variable.
我查看了其他代码示例,并且经常使用getter公开变量setter,他们也在使用 @property
。我错过了什么吗?我是Objective-C的新手,所以我不妨这样做!
I've looked at other code examples and often they're exposing the variable with a getter and setter, and they are also using @property
. Am I missing something? I'm new to Objective-C, so I might as well just be!
我做了 Apple + Shift + F for maxSprites在Project,Textual,Contains,Ignore Case中仅产生:
I did a Apple + Shift + F for maxSprites" In Project, Textual, Contains, Ignore Case and only resulted in:
Game.h: int maxSprites;
Game.m: maxSprites = 18;
Game.m: NSLog(@" maxSprites = %d", maxSprites);
Game.m: NSLog(@" maxSprites = %d", maxSprites);
Game.m: NSLog(@"maxSprites is at %p", &maxSprites);
Game.m: NSLog(@"maxSprites is at %p", &maxSprites);
Game.m: NSLog(@" maxSprites = %d", maxSprites);
Game.m: NSLog(@" maxSprites = %d", maxSprites);
Game.m: NSLog(@"maxSprites is at %p", &maxSprites);
Game.m: NSLog(@"maxSprites is at %p", &maxSprites);
我使用观察点找到了它所改变的位置。它改变了
I found the location where it changes using a watchpoint. It changes
Expression: "*(int *) 67379960"
New Value: 2
Old Value: 18
此行:
[self checkMatchBarAward:spriteTypeToAdd];
奇怪?该函数对 maxSprites
没有任何作用,也没有。
Odd? That function doesn't do anything with maxSprites
, nor does that line.
编辑:
这里是函数,我评论了它里面的一切,它仍然发生:
.h
here is the function, I commented everything inside it out and it still occurs: .h
-(void)checkMatchBarAward:(int)spriteTypeToAdd;
.m
-(void)checkMatchBarAward:(int)spriteTypeToAdd
{
}
编辑:
感谢您的推荐。我已清理所有目标,但仍然更改了值。由于你们/ gals给出的答案,你引导我解决问题。感谢您的所有帮助。
Thanks for the recommendations. I have cleaned all targets and it still changed values. Because of the answers you guys/gals gave, you lead me to the problem. Thanks for all of your help.
我在下面的答案中发布了我的结果。这是一份副本:
I posted my results below in an answer. Here's a copy:
你不相信原因是什么/伙伴。感谢您的帮助,因为我告诉我要清理并查看并查看我的类型,这有帮助。
Guys/gals you wouldn't believe what was the cause. Thank you for all your help because telling me to clean and look and check my types, that helped.
我查看了我的数组。我发现了一个声明如下:
I looked over my arrays. I found one that was declared like this:
int matchBarArray[8];
从断点处停止2行,它表示值从18变为2,我有这一行:
2 lines up from the breakpoint halt where it says that the value changed from 18 to 2, I have this line:
matchBarArray[spritesCaptured-1] = spriteTypeToAdd;
猜猜看,我超越了数组的界限1.如果我增加了数组的大小数组为9,我不再将int更改从18更改为2.
And guess what, I overstepped the bounds of the array by 1. If I increase the size of the array to 9, I no longer get the int change from 18 to 2.
此外,如果我超出边界超过1,即减少数组从小到大,还有其他的事情开始改变,比如我的得分,布尔,整场比赛! =)
Also, if I overstep the bounds by more than 1, that is, I reduce the array size to smaller, there are other things that start changing such as my score, booleans, the whole game ! =)
我无法相信在Objective-C中击中阵列外的内存可能导致这样的暴乱=)而且它很难调试!
I can't believe hitting memory outside the array in Objective-C can cause such a riot =) AND IT'S SO HARD TO DEBUG!
推荐答案
伙计们,你不会相信原因是什么。感谢您的帮助,因为我告诉我要清理并查看并查看我的类型,这有帮助。
Guys/gals, you wouldn't believe what was the cause. Thank you for all your help because telling me to clean and look and check my types, that helped.
我查看了我的数组。我发现了一个声明如下:
I looked over my arrays. I found one that was declared like this:
int matchBarArray[8];
从断点处停止2行,它表示值从18变为2,我有这一行:
2 lines up from the breakpoint halt where it says that the value changed from 18 to 2, I have this line:
matchBarArray[spritesCaptured-1] = spriteTypeToAdd;
猜猜看,我超越了数组的界限1.如果我增加了数组的大小数组为9,我不再将int更改从18更改为2.
And guess what, I overstepped the bounds of the array by 1. If I increase the size of the array to 9, I no longer get the int change from 18 to 2.
此外,如果我超出边界超过1,即减少数组从小到大,还有其他的事情开始改变,比如我的得分,布尔,整场比赛! =)
Also, if I overstep the bounds by more than 1, that is, I reduce the array size to smaller, there are other things that start changing such as my score, booleans, the whole game ! =)
我无法相信在Objective-C中击中阵列外的内存可能导致这样的暴乱=)而且它很难调试!
I can't believe hitting memory outside the array in Objective-C can cause such a riot =) AND IT'S SO HARD TO DEBUG!
这篇关于Objective-C:int值无变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!