我在学习目标-c,但我不明白这一点。我声明了一个nsstring i-var,我在init方法中设置了该值,然后当我在后面的实例方法中访问该ivar时,它崩溃或行为不可预知。NSString实例变量崩溃
//heres what my declaration looks like
@interface StockData : CCNode {
NSString *myPath;
NSString *myPath2;
}
-(id) init
{
if ((self = [super init])){
myPath = [[NSBundle mainBundle] pathForResource:@"stocks" ofType:@"sqlite"];
myPath2 = @"test";
CCLOG(@"mypath::::%@",[myPath class]);
CCLOG(@"mypath2::::%@",[myPath2 class]);
}
return self;
}
-(void) getChunk{
CCLOG(@"mypath_getchunk::::%@",[myPath class]);//this crashes
CCLOG(@"mypath2_getchunk::::%@", [myPath2 class]);//this doesn't
....
我使用cocos2d的,而我在这样一个计划的更新方法调用GetChunk方法:
-(void) updateOncePerSecond:(ccTime)delta{
if(!sd){
sd = [StockData initStockData];
[self addChild:sd];
}
[sd getChunk];
NSLog([sd getDate]);
}
它第一次通过迭代,我得到这样的:
2012-03 -19 20:33:58.591 HelloWorld [6777:10a03] mypath_getchunk :::: __ NSCFString
2012-03-19 20:33:58.591 HelloWorld [6777:10a03] mypath2_getchunk :::: __ NSCFConstantString
第二次迭代了通过(如果它不崩溃):
2012-03-19 20:33:59.589的HelloWorld [6777:10a03] mypath_getchunk :::: NSMallocBlock
2012-03 -19 20:33:59.589 HelloWorld [6777:10a03] mypath2_getchunk :::: __ NSCFConstantString
为什么它有时会崩溃,而不是其他时间。为什么它变成一个mallocblock?是NSString的马车,还是我做错了。其他变量似乎工作正常?我如何让我的NSCFString行为像NSCFConstantString。我更喜欢那个,因为它不会崩溃。任何建议将非常感谢! 谢谢!
您使用ARC,垃圾回收或保留/发布的内存管理是什么? – Mark 2012-03-19 12:51:35