Ios NSMutable数组在以其他方法访问时崩溃 -(无效)Aray { NSMutableArray*ColorArray=[[NSMutableArray alloc]init]; if(计数器
问题在于Ios NSMutable数组在以其他方法访问时崩溃 -(无效)Aray { NSMutableArray*ColorArray=[[NSMutableArray alloc]init]; if(计数器,ios,iphone,arrays,xcode,nsmutablearray,Ios,Iphone,Arrays,Xcode,Nsmutablearray,问题在于ColorArray需要是类的实例变量(或@property),以便它在方法调用之外保持不变 无论索引的值是多少,此代码将始终崩溃: -(void)Aray { NSMutableArray *ColorArray = [[NSMutableArray alloc] init]; if(Counter < NewColor) { [ColorArray addObject:[NSNumber numberWithInteger:ColorTe
ColorArray
需要是类的实例变量(或@property
),以便它在方法调用之外保持不变
无论索引的值是多少,此代码将始终崩溃:
-(void)Aray
{
NSMutableArray *ColorArray = [[NSMutableArray alloc] init];
if(Counter < NewColor)
{
[ColorArray addObject:[NSNumber numberWithInteger:ColorTemp]];
Counter += 1;
}
}
-(IBAction)Go:(id)sender
{
NSMutableArray *ColorArray = [[NSMutableArray alloc] init];
Color = [[ColorArray objectAtIndex:Index] intValue];
if(Color == 2)
{
ColorLabel.text = @"The Color is Black";
Screen.image = [UIImage imageNamed:@"BlackTile.png"];
}
else
{
Screen.image = [UIImage imageNamed:@"Tunnel.png"];
ColorLabel.text = @"The Color is Green";
}
Index += 1;
}
-(IBAction)Black:(id)sender
{
ColorTemp = 2;
NewColor += 1;
[self Array];
}
-(IBAction)Green:(id)sender
{
ColorTemp = 1;
NewColor += 1;
[self Array];
}
Color
似乎已经是一个实例变量(或@property
),因此这个概念对您来说不应该是陌生的
旁注:变量通常以小写开头,并使用camal大小写命名。它会在哪一点崩溃等etc2014-10-02 01:09:49.694测试[49737:60b]***由于未捕获的异常“NSRangeException”终止应用程序,原因:“***-[\u NSArrayM objectAtIndex:]:索引0超出空数组的界限”非常感谢您的指导。老实说,我是个新手,大约3周前才开始学习编程。因此,我的基础知识仍然很混乱。我做了一些研究,仍然不知道如何将数组作为实例变量。请帮忙。。非常感谢
NSMutableArray *ColorArray = [[NSMutableArray alloc] init];
Color = [[ColorArray objectAtIndex:Index] intValue];