iOS删除表单可变数组异常

Rom*_*-MT 0 nsmutablearray ios

我试图从我的数组中删除一些东西,但我得到了异常

删除时无法识别的选择器

我的代码:

NSMutableArray* tempNewsArray = [[NSMutableArray alloc]init];
tempNewsArray = [[defaults objectForKey:@"NewsArray"]mutableCopy];
for(int i = 0;i< [tempNewsArray count]-1;i++)
{
if( (monthIntofNew - month <= 10 && monthIntofNew - month !=0) || ( monthIntofNew - month <= -2))
    {
        [tempNewsArray delete:[tempNewsArray objectAtIndex:i]];//exception occurs here.
        //deleted
    }
}
Run Code Online (Sandbox Code Playgroud)

我想我错过了什么,请帮忙吗?

小智 5

请试试这个,

int indexNum=-1;
NSMutableArray* tempNewsArray = [[NSMutableArray alloc]init];
tempNewsArray = [[defaults objectForKey:@"NewsArray"]mutableCopy];
for(int i = 0;i< [tempNewsArray count]-1;i++)
{
if( (monthIntofNew - month <= 10 && monthIntofNew - month !=0) || ( monthIntofNew - month <= -2))
    {
       indexNum=i;
        //[tempNewsArray delete:[tempNewsArray objectAtIndex:i]];//exception occurs here.
        //deleted
    }
}

if(indexNum!=-1)
{
[tempNewsArray removeObjectAtIndex:indexNum];
}
Run Code Online (Sandbox Code Playgroud)

希望这会帮助你.