0

I want to check that objects from array is same or not and if yes then i want to merge element. For that I have done this but I am getting error that :

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArray replaceObjectAtIndex:withObject:]: mutating method sent to immutable object'

Array is:

(
    (
    hitesh,
    121231,
    Hitesh,
    Patel,
    3,
    1,
    0,
    0,
    TGB
),
    (
    hitesh,
    121231,
    Hitesh,
    Patel,
    3,
    1,
    0,
    0,
    "The Fern"
)
 )

My Code is:

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

Another issue is that After Completing this process I want to navigate page. Thats why I have written this method:

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

and I am doing this:

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

Should I have to return anything from method?

prajapatijayesh
  • 339
  • 5
  • 13

1 Answers1

0

Looks like you need to search in stackaOverflow before posting a question.

Other similar posts:

Fastest way here

Also you need to set your completion block to invoke it. Refer this post for more on blocks.

Adding code related to Blocks & Operations. So your implementation should go like this:

Method Signature :

-(void)compute:(NSMutableArray *)ary completion:(void (^)(BOOL))completion;

Method Definition :

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

Now call it like this :

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

Hope you got, how to work with that blocks

Community
  • 1
  • 1
Balram Tiwari
  • 5,657
  • 2
  • 23
  • 41
  • hello balram..I complete this but I have another issue: – prajapatijayesh Dec 18 '13 at 06:22
  • So as per your edits, it will complete the calculations & then will load the new view controller. So whats your problem then ? Do you want to pass that array to your new view controller. ?? – Balram Tiwari Dec 18 '13 at 06:31
  • My array is passing but it is not navigating... it is not entering the completion handler. – prajapatijayesh Dec 18 '13 at 06:32
  • Where is your `SearchResultVC` initialized ? Initialize it inside the completion block & try. `UIViewController *SearchResultVC = [UIViewController alloc]init]; [self.navigationController pushViewController:SearchResultVC animated:YES];` – Balram Tiwari Dec 18 '13 at 06:34
  • no it is out side completion block but it is not giving me error of that..I have written NSLog in completion block but it is not printing that as wll – prajapatijayesh Dec 18 '13 at 07:10
  • In your method you should tell when the completion is done. Try to get that from this [post](http://stackoverflow.com/questions/7180552/implementing-a-method-taking-a-block-to-use-as-callback) – Balram Tiwari Dec 18 '13 at 07:32
  • didnt get from that post – prajapatijayesh Dec 18 '13 at 08:49