Objective-C:你如何声明和保留int? [英] Objective-C: How do you declare and retain an int?
问题描述
这是我的代码:
@interface Game : Layer // this is from cocos2d
{
int maxSprites;
}
@implementation Game
-(void)initVariables
{
maxSprites = 18;
}
稍后,当我打印出来时,
Later on, when I print it out,
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!
编辑:
hrmph,为什么我得到-1?
hrmph, why'd I get a -1?
谢谢,我会尝试学习如何做一个观察点。
Thanks, I will try to learn how to do a Watchpoint.
在那之前,我想说我为maxSprites做了 APPLE + SHIFT + F 在Project,Textual,Contains,Ignore Case中只产生了:
Until then, I would like to say that 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);
2nd编辑:
我使用观察点找到了它所改变的位置。它在这里改变:
I found the location where it changes using a watchpoint. It changes here:
Expression: "*(int *) 67379960"
New Value: 2
Old Value: 18
在这一行:
[self checkMatchBarAward:spriteTypeToAdd];
奇怪?该函数对 maxSprites
没有任何作用。
Odd? That function doesn't do anything with maxSprites
.
编辑:
-我现在要提出一个新问题,找出价值自身发生变化的原因。谢谢你们的帮助,干得好。
-I'm going to make a new question now to find out why the value is changing on its own. Thank you for your help guys, great job.
这里将收到新帖子:
Objective-C:int值无变化
推荐答案
您不保留int,因为它不是对象。使用观察点并找出变量何时发生变化。
You don't retain an int because it's not an object. Use a watchpoint and find out when your variable is changing.
这篇关于Objective-C:你如何声明和保留int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!