我想初始化一个数组并从定时器循环访问其内容 但程序失败了。我认为问题在于定义阵列,但我找不到解决方案。我的代码是:
头文件:
NSMutableArray *myArray;
m file:
+(NSMutableArray *) myArray
{
if (!myArray) {
myArray= [[NSMutableArray alloc]init];
}
return myArray;
}
-(id) init
{
if( (self=[super init])) {
myArray=[NSMutableArray arrayWithObjects:[NSValue valueWithCGPoint:CGPointMake(5,6)],
[NSValue valueWithCGPoint:CGPointMake(50,16)],
[NSValue valueWithCGPoint:CGPointMake(55,20)],
[NSValue valueWithCGPoint:CGPointMake(95,30)],
[NSValue valueWithCGPoint:CGPointMake(135,40)],
[NSValue valueWithCGPoint:CGPointMake(175,55)],
nil];
}
return self;
}
和计时器循环:
-(void)targetmethod{
// It fails here
NSValue * val = [myArray objectAtIndex:2];
}