根据子NSMutableDictonary的键值从NSMutableArray中检索对象

时间:2020-12-30 23:09:28

So I have an NSMutableArray in my app what is filled like so:

所以我在我的应用程序中有一个NSMutableArray,如下所示:

NSMutableArray
--Object 1 (NSMutableDictonary)
  --Value (i) for key (key)
  --Value (i) for key (key)
--Object 2 (NSMutableDictonary)
  --Value (i) for key (key)
  --Value (i) for key (key)

i need to be able to select an object from within the main NSMutableArray by looking for a key what matches a value of one of the NSMutableDictonary's keys...

我需要能够通过查找与NSMutableDictonary键之一的值匹配的键来从主NSMutableArray中选择一个对象...

I understand i could run a loop like so to achieve this:

我明白我可以像这样运行一个循环来实现这个目的:

for (NSMutableDictionary *object in arrayObject) {
    if ([[object objectForKey:@"keyToSearch"] integerValue] == keyToCompare) {
        return [object objectForKey:@"keyToReturn"];
    }
}

However my concern is that if this array grows (what it can do) then this will take time to run a search on it..

但是我担心的是,如果这个数组增长(它可以做什么),那么这需要时间来对它进行搜索。

So I was wondering is there any other way to retrieve the same results but more efficiently?

所以我想知道有没有其他方法来检索相同的结果,但更有效?

Thanks

Liam

2 个解决方案

#1


1  

The search is obviously going to take linear time in the number of dictionaries in the array. You're not going to get around that by using a different way to search. You could rewrite your search to use an NSPredicate, and it would be slightly shorter but it wouldn't be any faster.

搜索显然需要在数组中的字典数量中采用线性时间。你不会通过使用不同的搜索方式解决这个问题。您可以重写您的搜索以使用NSPredicate,它会稍微缩短但不会更快。

If you want to make the search faster, you'll have to modify your data structure. Or, you could create a NSDictionary to serve as a lookup table, where the keys of the NSDictionary are values of keyToSearch, and the values are the NSMutableDictionaries in your array.

如果您想更快地进行搜索,则必须修改数据结构。或者,您可以创建一个NSDictionary作为查找表,其中NSDictionary的键是keyToSearch的值,值是数组中的NSMutableDictionaries。

Still, unless your array gets really really, big the search time will be negligible, and you shouldn't worry about it.

尽管如此,除非您的阵列确实非常真实,否则搜索时间可以忽略不计,您不必担心它。

#2


0  

Ultimately, any enumeration of any array will take time that grows as the size of the array grows. If you have any way to hint at the location of it, it may make it faster, but otherwise, Fast Enumeration is probably the fastest way to run through the data in the way that you want to.

最终,任何数组的枚举都会花费时间,随着数组大小的增长而增长。如果你有任何方法提示它的位置,它可能会使它更快,但除此之外,快速枚举可能是以你想要的方式运行数据的最快方法。

Unless you run through hundreds of objects, the time taken should be negligible.

除非你经历了数百个对象,否则所用的时间应该可以忽略不计。

#1


1  

The search is obviously going to take linear time in the number of dictionaries in the array. You're not going to get around that by using a different way to search. You could rewrite your search to use an NSPredicate, and it would be slightly shorter but it wouldn't be any faster.

搜索显然需要在数组中的字典数量中采用线性时间。你不会通过使用不同的搜索方式解决这个问题。您可以重写您的搜索以使用NSPredicate,它会稍微缩短但不会更快。

If you want to make the search faster, you'll have to modify your data structure. Or, you could create a NSDictionary to serve as a lookup table, where the keys of the NSDictionary are values of keyToSearch, and the values are the NSMutableDictionaries in your array.

如果您想更快地进行搜索,则必须修改数据结构。或者,您可以创建一个NSDictionary作为查找表,其中NSDictionary的键是keyToSearch的值,值是数组中的NSMutableDictionaries。

Still, unless your array gets really really, big the search time will be negligible, and you shouldn't worry about it.

尽管如此,除非您的阵列确实非常真实,否则搜索时间可以忽略不计,您不必担心它。

#2


0  

Ultimately, any enumeration of any array will take time that grows as the size of the array grows. If you have any way to hint at the location of it, it may make it faster, but otherwise, Fast Enumeration is probably the fastest way to run through the data in the way that you want to.

最终,任何数组的枚举都会花费时间,随着数组大小的增长而增长。如果你有任何方法提示它的位置,它可能会使它更快,但除此之外,快速枚举可能是以你想要的方式运行数据的最快方法。

Unless you run through hundreds of objects, the time taken should be negligible.

除非你经历了数百个对象,否则所用的时间应该可以忽略不计。

相关文章