【发布时间】:2012-04-26 20:11:08
【问题描述】:
我试图在一个连续运行的函数中创建一个 NSMutableArray,并且只初始化一次它的值,这样它就不会在重复调用函数时继续初始化值(从而替换更改的值)。我的问题是,当我尝试在 if 语句中初始化值时,数组中的值不会改变,当我期望它打印“值为 1”时,它会继续打印“值为 0”
这是我的相关代码:
@property (nonatomic, strong) NSMutableArray * shapeMarked;
@synthesize shapeMarked;
//locationManager is the function that's continuously called
-(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
//event count is an integer that continuously increases by one each time the
//parent function is called, this if statement is used so that it only happens
//once
if(eventcount == 1){
for (int i = 0; i < 5; i++){
BOOL b = YES;
[shapeMarked addObject:[NSNumber numberWithBool:b]];
NSLog(@"value is %d", [[shapeMarked objectAtIndex:i] boolValue] );
}
}
}
【问题讨论】:
-
eventcount == 1是否匹配过?在这种情况下添加一个 NSLog。另外,shapeMarked 实际上是一个有效的 NSArray 吗?添加NSLog(@"array: %@", shapeMarked); -
当我添加 NSLog(@"array ... ); 它正在输出 "array: (null).我知道“事件计数 == 1”是匹配的,因为 NSLog 输出,如果它不匹配,我不会得到输出,因为 NSLog 在 if 语句中被调用。
标签: objective-c nsmutablearray nsarray boolean