【问题标题】:How to perform Array Comparison & merging [duplicate]如何执行数组比较和合并[重复]
【发布时间】:2013-12-18 06:08:10
【问题描述】:

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

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“-[__NSCFArray replaceObjectAtIndex:withObject:]: mutating method sent to immutable object”

数组是:

(
    (
    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);
 }

另一个问题是,完成此过程后,我想导航页面。这就是我写这个方法的原因:

-(void)compute:(NSMutableArray *)ary completion:(void (^)(void))completion{ }

我正在这样做:

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

我应该从方法中返回任何东西吗?

【问题讨论】:

  • 您的问题与您的详细查询不同。请您正确地重新提问。
  • @mAc..是什么意思??我没有得到你....
  • 你的 searchResultArray 是可变的吗?
  • [self compute: ?后面的代码是什么

标签: ios iphone nsmutablearray nsarray


【解决方案1】:

看来您需要先搜索stackaOverflow,然后才能发布问题。

其他类似帖子:

Fastest way here

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

添加与块和操作相关的代码。所以你的实现应该是这样的:

方法签名:

-(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];
    }];

希望你明白,如何使用 blocks

【讨论】:

  • hello balram..我完成了这个,但我还有另一个问题:
  • 因此,根据您的编辑,它将完成计算,然后加载新的视图控制器。那你的问题是什么?你想将该数组传递给你的新视图控制器吗? ??
  • 我的数组正在传递,但它没有导航......它没有进入完成处理程序。
  • 你的SearchResultVC在哪里初始化?在完成块内初始化它并尝试。 UIViewController *SearchResultVC = [UIViewController alloc]init]; [self.navigationController pushViewController:SearchResultVC animated:YES];
  • 不,它在完成块之外,但它没有给我错误..我已经在完成块中写了 NSLog,但它没有打印出来
猜你喜欢
  • 1970-01-01
  • 2013-11-26
  • 2021-05-13
  • 1970-01-01
  • 2021-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多