NSMutable数组removeAllObject也删除其他NSMutable数组中的对象

ven*_*nky 0 arrays objective-c nsmutablearray ios

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

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];
Run Code Online (Sandbox Code Playgroud)

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

Abh*_*hek 5

你应该分配喜欢

localArray = [NSArray arrayWithArray:responseArray];

  • 或localArray = [responseArray mutableCopy] (2认同)