Update/change 存储在 NSArray 中的 NSDictionary 键的值

Update/change values of NSDictionary keys stored in NSArray

我有一个 NSArray 包含 NSDictionary 的客户数据。我正在使用 NSPredicate 过滤 array 并通过 ViewControllerssegues 传递它。我正处于想要更改 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 现在仅包含基于此 predicate 的某些 dictionaries(即,当用户在 TableView 上选择一行时,假设 'Manchester' , 然后只有 Clinic_LocationManchesterdictionaries 被传递到下一个视图。

我在下一个 TableView 上的 prepareForSegue 方法与上一个类似,在这种情况下,我的 predicate 过滤 dictionary 与所选姓氏匹配的 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;
    }
)

我尝试了很多不同的方法来更新此 array 中的特定 dictionary keys,例如使用 predicates,但似乎没有任何效果。例如:

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

无论我尝试什么,我都会不断收到错误消息,例如:

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFString 0x7904f590> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key Client_Address_Line_1.'

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryI setObject:forKey:]: unrecognized selector sent to instance 0x78774c70'

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSDictionaryI 0x7bff6eb0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key Client_Address_Line_1.'

似乎无论我做什么我都无法更改或更新 array 中的任何 dictionary keys。经过数小时的研究,我开始怀疑这是否可能?我是否完全看错了方法?有一个更好的方法吗? keys 更改后,我需要将它们保存回 array 并通过 ViewControllers 传回(可能使用 Unwind Segues?)非常感谢任何帮助.

您只能更改 NSDictionary 的可变子类中的键值,NSDictionary 本身是不可变的。

因此,由于您已更改为可变数组,因此您也可以对每个词典执行相同的操作:

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