NSMutablearray removeAll Object也删除其他NSMutablearray中的对象

时间:2014-01-03 05:15:23

标签: ios objective-c arrays nsmutablearray

我有三个类,即 DepartmentViewController DepartmentRequest 部门。它就像我从 DepartmentViewcontroller 发出部门请求,并基于响应我操纵repsonse并在 DepartmentRequest 类中添加NSmutableArray * responseArray 。该数组只包含Department对象。我想在 DepartmentViewController 中使用 responseArray 来搜索和重新加载tableview。所以我通过委托将此数组传递给 DepartmentViewController ,并将 responseArray 分配给 localArray 。现在我根据这两个数组进行搜索,但是如果我使用 removeallobject 删除任何一个数组。它也在其他数组中删除对象。

if(searchString.length>0)
  {

      [localArray removeAllObjects];
    for (int i =0 ; i < [departmentRequest.responseArray count]; i++) {
      Department *dept = [departmentRequest.responseArray objectAtIndex:i];

        if (([dept.departmentName rangeOfString:searchString options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)].location != NSNotFound)
          )

        {
                   [localArray addObject:dept];
        }
        else{
            NSLog(@"deptname %@",dept.departmentName);
        }

    }

    [departmentTableView reloadData];

如果我删除localArray中的对象,则删除departmentReqeust.responseArray和localArray中的对象

2 个答案:

答案 0 :(得分:5)

您应该指定

  

localArray = [NSArray arrayWithArray:responseArray];

答案 1 :(得分:0)

如果(searchString.length大于0)   {

 // [localArray removeAllObjects];

 NSMutableArray arrTemp=[[NSMutableArray alloc]init];

for (int i =0 ; i < [departmentRequest.responseArray count]; i++) {
  Department *dept = [departmentRequest.responseArray objectAtIndex:i];

    if (([dept.departmentName rangeOfString:searchString options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)].location != NSNotFound)
      )

    {
               [arrTemp addObject:dept];
    }
    else{
        NSLog(@"deptname %@",dept.departmentName);
    }

}
localArray = [NSArray arrayWithArray:arrTemp];

[departmentTableView reloadData];