我有一个NSArray
包含NSDictionary
个客户端数据。我使用NSPredicate
来过滤array
,并使用ViewControllers
将其传递给segues
。我正处于一个阶段,我想更改dictionary
中存储的密钥的某些值。这怎么可以实现?
我的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
现在只包含基于此dictionaries
的某些predicate
(即,当用户在TableView
上选择一行时,假设为'Manchester'
,则只有dictionaries
Clinic_Location
的{{1}}传递到下一个视图。
下一个Manchester
上的prepareForSegue
方法与上一个方法类似,在这种情况下,我的TableView
会过滤predicate
所选行上匹配的姓氏,将其推送到下一个视图。现在我留下的dictionary
构造如下:
array
我尝试了许多不同的方法来更新此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;
}
)
中的特定dictionary keys
,例如使用array
,但似乎没有任何效果。例如:
predicates
无论我尝试什么,我都会不断收到错误消息,例如:
***由于未捕获的异常'NSUnknownKeyException'而终止应用程序,原因:'[< __ NSCFString 0x7904f590> setValue:forUndefinedKey:]:this class不是密钥的编码兼容键值 Client_Address_Line_1'。
***由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [__ NSDictionaryI setObject:forKey:]:无法识别的选择器发送到实例0x78774c70'
***由于未捕获的异常'NSUnknownKeyException'而终止应用程序,原因:'[< __ NSDictionaryI 0x7bff6eb0>的setValue:forUndefinedKey:]: 此类不是密钥值编码兼容的密钥 Client_Address_Line_1'。
无论我做什么,我都无法更改或更新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"];
中的任何dictionary keys
。经过几个小时的研究,我开始怀疑这是否可能?我完全看错了吗?有一个更好的方法吗?更改array
后,我需要将它们保存回keys
并通过array
传回(可能还有ViewControllers
?)非常感谢任何帮助
答案 0 :(得分:10)
您只能更改NSDictionary的可变子类中键的值,NSDictionary本身是不可变的。
所以既然你已经改成了一个可变数组,你也可以对每个字典做同样的事情:
filteredClientArray = [NSMutableArray arrayWithArray:sortedArray];
for(NSDictionary *d in [filteredClientArray copy])
{
NSMutableDictionary * m = [d mutableCopy];
[filteredClientArray replaceObjectAtIndex:[filteredClientArray indexOfObject:d] withObject:m];
}