Objective-C: I need help storing the value of int. This does not change me without my team.
The initial question was βHow do you declare and save an int?β Which was satisfied in another post here: Objective-C: How do you declare and save an int?
Now I have a problem when the int, which was 18, changes to 2, somehow on its own.
Here is my code:
@interface Game : Layer // this is from cocos2d
{
int maxSprites;
}
@implementation Game
-(void)initVariables
{
maxSprites = 18;
}
Later, when I print it, for example
NSLog(@" maxSprites = %d ", maxSprites);
I get:
maxSprites = 2
And operations that require it to be 18 are crashing or not working, as if it were really only 2 now.
How is this possible? =)
Apple+ Shift+ FShows another use of the variable maxSprites.
, getter setter, @property. - ? Objective-C, !
Apple + Shift + F maxSprites " Project, Textual, Contains, Ignore Case :
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);
, .
Expression: "*(int *) 67379960"
New Value: 2
Old Value: 18
:
[self checkMatchBarAward:spriteTypeToAdd]
? maxSprites, .
EDIT:
, , , :
.h
-(void)checkMatchBarAward:(int)spriteTypeToAdd;
.m
-(void)checkMatchBarAward:(int)spriteTypeToAdd
{
}
EDIT:
. . - , , /gals, . .
. :
Guys / gala you will not believe what the reason is. Thank you for your help, because I say that I clean and look and check my types that helped.I looked through my arrays. I found one that was declared as follows:
int matchBarArray[8];
2 lines from the stop of the breakpoint, where it says that the value has changed from 18 to 2, I have this line:
matchBarArray[spritesCaptured-1] = spriteTypeToAdd;
And guess that I stepped over the boundaries 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.
In addition, if I stepped over the boundaries by more than 1, that is, I reduce the size of the array to a smaller size, there are other things that are starting to change, such as my score, logical value, the whole game! =)
I canβt believe that getting memory outside the bounds of an array in Objective-C can cause such a riot =) AND THIS IS SUCH A DIFFICULT DEBUG!