2015-01-27 1 views
0

의 클라이언트 데이터가 포함 된 NSArray이 있습니다. array을 필터링하고 ViewControllerssegues으로 전달하려면 NSPredicate을 사용하고 있습니다. 나는 dictionary에 저장된 키의 일부 값을 변경하고자하는 단계에 있습니다. 어떻게 이것을 달성 할 수 있습니까?NSArray에 저장된 NSDictionary 키의 업데이트/변경

prepareForSegue 방법

if ([[segue identifier] isEqualToString:@"showClients"]) { 
      // Detect selected row 
      NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
      // Extract value from the Clinic_Location attribute 
      NSString *cString = [[_clinicList valueForKey:@"Clinic_Location"] objectAtIndex:indexPath.row]; 
      NSLog(@"Row pressed: %@", cString); 

      // Set predicate where Clinic_Location equals the clinic string extracted from the selected row 
      NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Clinic_Location == %@", cString]; 
      // Filter the client array using this predicate 
      filteredClientArray = [dataStart filteredArrayUsingPredicate:predicate]; 
      NSLog(@"Filtered array: %@", filteredClientArray); 
      // Reload the table 
      [self.tableView reloadData]; 

      // Sort the array by Appt_Time in ascending order 
      NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]initWithKey:@"Appt_Time" ascending:YES]; 
      NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil]; 
      NSArray *sortedArray = [filteredClientArray sortedArrayUsingDescriptors:sortDescriptors]; 
      filteredClientArray = [NSMutableArray arrayWithArray:sortedArray]; 

      // Pass the newly filtered & sorted array to next view 
      PCClientsViewController *pcClients = segue.destinationViewController; 
      pcClients.clientArray = filteredClientArray; 
     } 

array 이제 포함 predicate가 (사용자가 TableView에 행을 선택할 때,의는 ManchesterClinic_Location으로 다음 'Manchester'dictionaries을 가정 해 봅시다 즉 전달받을이에 따라 특정 dictionaries 다음보기로 이동하십시오.

prepareForSegue 방법은 다음에 TableView은 이전 것과 비슷합니다.이 경우에는 내 predicate은 선택한 행의 성이 일치하는 dictionary을 필터링하고 다음보기로 푸시합니다. 지금은 아래와 같이 구성되는 array 왼쪽 오전 : 나는 predicates를 사용하여 등이 array 특정 dictionary keys를 업데이트하는 여러 가지 방법을 시도했지만 아무것도 작동하는 것 같다되지

Filtered array: (
     { 
     "Appointment_Attended" = Yes; 
     "Appt_Time" = "2:00"; 
     "Client_Address_Line_1" = "Ap #452-4253 Massa. Street"; 
     "Client_Address_Line_2" = Montebello; 
     "Client_Address_Line_3" = Hull; 
     "Client_Address_Line_4" = Quebec; 
     "Client_Address_Line_5" = Micronesia; 
     "Client_Forename" = Veronica; 
     "Client_Postcode" = 69591; 
     "Client_Surname" = Ellis; 
     "Client_Tel" = "(019376) 26081"; 
     "Clinic_Location" = Manchester; 
     "Passed_To_Medical" = No; 
     "Passed_To_Sol" = No; 
    } 
) 

. < __NSCFString 0x7904f590 '[:

응용 프로그램을 종료

*** 인해 캐치되지 않는 예외'NSUnknownKeyException ', 이유에 : 아무리 내가 계속 같은 오류 메시지가 무엇입니까 무엇을하려고

NSPredicate* predicate = [NSPredicate predicateWithFormat:@"Client_Address_Line_1 == %@", [[_clientDetails valueForKey:@"Client_Address_Line_1"]objectAtIndex:0]]; 
NSLog(@"%@", predicate); 
NSArray* filteredArray= [_clientDetails filteredArrayUsingPredicate: predicate]; 
[filteredArray performSelector: @selector(setValue:forKey:) withObject:self.clientAddLine1.text withObject:@"Client_Address_Line_1"]; 

: 예를 들어, > setValue : forUndefinedKey :] :이 클래스는 키Client_Address_Line_1에 대해 키 값 코딩을 준수하지 않습니다. '

*** 인해 캐치되지 않는 예외 'NSInvalidArgumentException'응용 프로그램 종료, 이유는 : '- [__ NSDictionaryI 의 setObject : forKey :] : 인식 할 수없는 선택기 예를 0x78774c70로 전송'응용 프로그램을 종료

*** 인해 캐치되지 않는 예외에 'NSUnknownKeyException', 이유 : '[< __NSDictionaryI 0x7bff6eb0> setValue : forUndefinedKey :] : 이 클래스는 키Client_Address_Line_1의 키 값을 코딩하지 않습니다.'

array 내에서 dictionary keys을 변경하거나 업데이트 할 수 없습니다. 몇 시간의 연구 끝에, 이것이 가능한지 궁금해지기 시작했습니다. 잘못된 접근 방식을 완전히보고 있습니까? 이 작업을 수행하는 더 좋은 방법이 있습니까? keys을 변경 한 후에는 array에 다시 저장하고 ViewControllers (아마도 Unwind Segues?)을 통해 다시 전달해야합니다. 도움이 필요합니다.

답변

6

NSDictionary의 변경할 수있는 하위 클래스의 키 값만 변경할 수 있습니다. NSDictionary 자체는 변경할 수 없습니다.

filteredClientArray = [NSMutableArray arrayWithArray:sortedArray]; 
for(NSDictionary *d in [filteredClientArray copy]) 
{ 
    NSMutableDictionary * m = [d mutableCopy]; 
    [filteredClientArray replaceObjectAtIndex:[filteredClientArray indexOfObject:d] withObject:m]; 
} 
+0

물론 수정 가능한 복사본이 유지됩니다, 당신이 경우에 대처해야합니다 :

는 변경 가능한 배열에 변경 그래서 때문에, 당신은 또한 사전의 각각에 대해 같은 일을 할 수 아크를 사용하고 있지 않다 –

+0

빠른 답장을 보내 주셔서 감사합니다. 'keys'가 변경 될 때마다 모든 사전을'변경 가능한 사전 '으로 바꿔야합니까? 아니면'array'가 마지막'ViewController'에 전달되기 전에 코드를 구현해야합니까? 또한'ARC'를 사용하고 있습니다. – rosshump

+1

더블 코멘트에 죄송합니다 - 데이터를 변경할 때 내 메소드에 코드를 추가하고 내 NSPredicate predicateWithFormat : @ "Client_Address_Line_1 == % @"'코드를 사용하여 사전 키를 성공적으로 변경할 수 있습니다. 고마워요! – rosshump

관련 문제