简体   繁体   English

在NSArray中更新NSDictionary

[英]Updating NSDictionary inside an NSArray

I have a NSArray of NSDictionaries . 我有NSDictionariesNSArray I need the updated NSArray of NSDictionaries with value of one of the dictionary key updated with new value. 我需要NSDictionaries的更新的NSArray ,其值之一是用新值更新的字典键之一。

Please see below the NSArray structure. 请参见下面的NSArray结构。 I want to update the value for Key3 inside this structure if Key1 matches with some value (eg 2). 如果Key1与某个值(例如2)匹配,我想在此结构内更新Key3值。 What would the fastest way of doing this. 最快的方法是什么? I do not want to use traditional For loop. 我不想使用传统的For循环。

[myArray valueForKeyPath:@"@unionOfObjects.Key3"];

<__NSCFArray 0xe70c10>(
{
    Key1 = 1;
    Key2 = "New Location";
    Key3 =     (
        Data1,
        Data2,
        Data3
    );
},
{
    Key1 = 2;
    Key2 = "Old Location";
    Key3 =     (
        Data1,
        Data2,
        Data4
    );
}
)

We can solve it by using predicates: 我们可以使用谓词来解决它:

NSArray *dictionaryArray;
NSNumber *key =@2;
NSPredicate *predicate =[NSPredicate predicateWithFormat:@"Key1=%@",key];
NSArray *result =[dictionaryArray filteredArrayUsingPredicate:predicate];

result array now has all dictionaries having (key1 = 2) 结果数组现在所有字典都具有(key1 = 2)

Dictionarys can't be edited directly, they must be NSMutableDictionary to edit. 字典不能直接编辑,它们必须是NSMutableDictionary才能编辑。 Assuming they are NSMutableDictionary instances: 假设它们是NSMutableDictionary实例:

NSMutableDictionary *dic =result[0];
[dic setObject:someObject forKey:@"key3"];

Let's suppose that you already have an array containing all mutable dictionaries. 假设您已经有一个包含所有可变字典的数组。 The first step is to get what dictionaries you need to change: 第一步是获取需要更改的词典:

NSPredicate* predicate= [NSPredicate predicateWithFormat: @"Key1=2"];
NSArray* filteredArray= [myArray filteredArrayUsingPredicate: predicate];

The second step is to replace the Key3 value for each object in the array. 第二步是替换数组中每个对象的Key3值。 If you execute a selector on an array, and NSArray doesn't respond to that selector, the selector is performed on it's objects: 如果在数组上执行选择器,而NSArray不响应该选择器,则选择器将在其对象上执行:

[filteredArray performSelector: @selector(setValue:forKey:) withObject: someValue withObject: @"Key3"];

After this statement you don't need to replace any object in myArray , because the filtered array contains the same objects that are in myArray , which are mutable dictionaries. 在此语句之后,您不需要替换myArray任何对象,因为过滤后的数组包含与myArray相同的对象,它们是可变字典。

I would also go for predicates as @santhu stated on his answer. 正如@santhu在他的回答中所说,我也会去做谓语。 However, I want to point that when searching an unsorted array linear search (that is, looping through every object and checking if it's the wanted one) is optimal. 但是,我想指出的是,在搜索未排序的数组时,线性搜索(即遍历每个对象并检查是否是所需对象)是最佳的。 Probably predicates will provide an speed-up due to their internal routines, but a predicate will still execute the "traditional for loop" you want to avoid and thus the speed-up will not be very significant. 谓词可能会由于其内部例程而提供加速功能,但是谓词仍会执行您要避免的“传统for循环”,因此加速效果不会非常明显。

There are other searching algorithms which provide a significant speed-up, but they are only valid for sorted databases ( unless you have a quantum iPhone ;) ). 还有其他搜索算法可以显着提高速度,但是它们仅对排序的数据库有效( 除非您有量子iPhone ;))。

If you are interested on this topic, here you can find some types of search algorithms with an analysis of their complexity. 如果您对此主题感兴趣,可以在这里找到一些类型的搜索算法,并对它们的复杂性进行分析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM