Ios 如何执行阵列比较&;合并

Ios 如何执行阵列比较&;合并,ios,iphone,nsmutablearray,nsarray,Ios,Iphone,Nsmutablearray,Nsarray,我想检查数组中的对象是否相同,如果是,我想合并元素。为此,我已经这样做了,但我得到的错误是: 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“-[\uu nsFarray replaceObjectAtIndex:withObject:::正在将方法发送到不可变对象” 数组是: ( ( hitesh, 121231, Hitesh, Patel, 3, 1, 0,

我想检查数组中的对象是否相同,如果是,我想合并元素。为此,我已经这样做了,但我得到的错误是:

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“-[\uu nsFarray replaceObjectAtIndex:withObject:::正在将方法发送到不可变对象”

数组是:

(
    (
    hitesh,
    121231,
    Hitesh,
    Patel,
    3,
    1,
    0,
    0,
    TGB
),
    (
    hitesh,
    121231,
    Hitesh,
    Patel,
    3,
    1,
    0,
    0,
    "The Fern"
)
 )
我的代码是:

-(void)compute:(NSMutableArray *)ary completion:(void (^)(void))completion{
int id=0;
NSLog(@"Array is%@",ary);
NSMutableArray *temp;
for(int i=0;i<[ary count];i++){
    temp=[ary objectAtIndex:i];
        NSLog(@"string%@",[temp objectAtIndex:8]);
    if(id==[[temp objectAtIndex:4] integerValue]){
        NSLog(@"inside");
        NSMutableArray *temp1=[[NSMutableArray alloc]init];
        temp1=[MyAppDelegate.searchResultArray objectAtIndex:i-1];
        NSLog(@"temp1%@",temp1);
        NSString *tempStr=[[temp1 objectAtIndex:8] stringByAppendingString:[temp objectAtIndex:8]];
        NSLog(@"%@",tempStr);
        [temp1 replaceObjectAtIndex:8 withObject:tempStr];
        [MyAppDelegate.searchResultArray replaceObjectAtIndex:i-1 withObject:temp1];
    }
    else{
    id=[[temp objectAtIndex:4] integerValue];
    NSLog(@"temp is%@ %d",temp,id);
    [MyAppDelegate.searchResultArray addObject:temp];
    }
  }
  NSLog(@"%@",MyAppDelegate.searchResultArray);
 }
我正在这样做:

[self compute:[root valueForKey:@"searchResult"] completion:^{
            NSLog(@"after co");
            [self.navigationController pushViewController:SearchResultVC animated:YES];
        }];

我是否需要从方法中返回任何内容?

看起来您需要在发布问题之前进行搜索

其他类似员额:

您还需要设置完成块来调用它。有关块的更多信息,请参阅此

添加与块和操作相关的代码。因此,您的实现应该如下所示:

方法签名:

-(void)compute:(NSMutableArray *)ary completion:(void (^)(BOOL))completion;
方法定义:

-(void)compute:(NSMutableArray *)ary completion:(void (^)(BOOL))completion{
    ...
    ...
    // perform your calculations & everything related to array comparison 
    ...
    // Mark the finish of the block as 
    BOOL finished = TRUE;
    if (completion) {
        completion(finished);
    }
}
现在这样称呼它:

[self compute:[root valueForKey:@"searchResult"] completion:^{
        NSLog(@"after co");
        [self.navigationController pushViewController:SearchResultVC animated:YES];
    }];

希望您能了解,如何处理

您的问题与您的详细查询不同。。你能正确地重新问这个问题吗?@mAc..means??我不明白你的意思…你的searchResultArray是可变的吗?后面的代码是什么[自我计算:?你好,balram..我完成了此操作,但我有另一个问题:根据您的编辑,它将完成计算,然后将加载新的视图控制器。那么您的问题是什么?您想将该数组传递给新的视图控制器吗?我的数组正在传递,但它没有导航…它没有进入完成处理程序。您的
SearchResultVC
在哪里初始化?在完成块内初始化它并重试。
UIViewController*SearchResultVC=[UIViewController alloc]init];[self.navigationController pushViewController:SearchResultVC动画:是]
不,它是外侧完成块,但它没有给我这个错误。我在完成块中写了NSLog,但它没有打印为wll
[self compute:[root valueForKey:@"searchResult"] completion:^{
        NSLog(@"after co");
        [self.navigationController pushViewController:SearchResultVC animated:YES];
    }];