0

I have three class namely DepartmentViewController, DepartmentRequest, Department. Its like im making a department request from DepartmentViewcontroller and based on response im manipulating repsonse and adding in NSmutableArray *responseArray in DepartmentRequest class. This array just contains Department object. I want this responseArray in DepartmentViewController for searching and reloading tableview. so i passed this array to DepartmentViewController through delegate and assigning responseArray to localArray. Now I did searching based on these two array but if i remove any one of the array using removeallobject. Its removing object in other Array too.

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

if i remove object in localArray its removing objects both in departmentReqeust.responseArray and localArray

2
  • are you directly assigning using localArray = responseArray ? Commented Jan 3, 2014 at 5:18
  • It sounds like you think you have two arrays and there is actually only one. You are likely passing a reference, not a clone. The code you have supplied is not enough to determine if that is the case or not. Commented Jan 3, 2014 at 5:20

2 Answers 2

5

You should assign like

localArray = [NSArray arrayWithArray:responseArray];

Sign up to request clarification or add additional context in comments.

2 Comments

localArray is also MutableArray, so I changed to [NSMutableArray arrayWithArray:responseArray]. thank you
or localArray = [responseArray mutableCopy]
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.