【发布时间】: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