使用多个条件搜索字典数组
我在 NSArray 中有很多 NSDictioanries 。由于它们大约有 1300 个,我需要根据以下条件搜索它们:
1. Name
2. DOB
3. Type
4. Grade
5. Condition
6. PUP
7. Unit Number
这是这样一个字典的示例:
<Unit>
<UnitNumber>20110501100507134</UnitNumber>
<Name>01'' 2ply Mat</Name>
<DOB>3/24/2011 12:00:00 AM</DOB>
<Type>2ply Mat</Type>
<Grade>Cull</Grade>
<Condition />
<Depth>01</Depth>
<Width>01</Width>
<Length>01</Length>
<PUP>Cable</PUP>
<Finishing />
</Unit>
现在我正在考虑 for 循环数组,搜索第一个条件,然后再次 for 循环通过过滤后的数组并搜索第二个标准等等...... 但这是 7 个 for 循环,遍历了至少 1300 个字典。
我有什么选择? 谢谢。
更新:好的,对于上面列出的每个条件,我将有一个文本字段,用户将在那里指定条件,然后按搜索按钮...(如果这让事情变得更清楚)
另外,这是为了iPhone/iPad
I have alot of NSDictioanries in an NSArray. Since there are about 1300 of them, and I need to search through them based of the following criteria:
1. Name
2. DOB
3. Type
4. Grade
5. Condition
6. PUP
7. Unit Number
Here's an example of such a dictionary:
<Unit>
<UnitNumber>20110501100507134</UnitNumber>
<Name>01'' 2ply Mat</Name>
<DOB>3/24/2011 12:00:00 AM</DOB>
<Type>2ply Mat</Type>
<Grade>Cull</Grade>
<Condition />
<Depth>01</Depth>
<Width>01</Width>
<Length>01</Length>
<PUP>Cable</PUP>
<Finishing />
</Unit>
Right now I am thinking about for looping through the array, search for first criterion, then for loop again through the filtered array and search for 2nd criterion and so on....
But that's 7 for loops, through at-least 1300 dictionaries.
What are my alternatives?
Thanks.
UPDATE: Ok so for each of the criteria i listed above, I am going to have text field, and user will specify the criteria there and then press the search button...(if that makes things more clear)
Also, this is for iPhone/iPad
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有一个简单的方法可以做到这一点,您可以使用 NSPredicate 根据您上面提到的条件搜索您的数组。
另外,如果您想以排序的方式显示该数组,您应该使用 NSSortDiscriptor。
Well there is an easy way to do it, you can use NSPredicate to search for your array based on the criteria you have mentioned above.
Also as a bonus, If you want to display that array in sorted manner you should use NSSortDiscriptor.
虽然@Robin的答案很准确,但您也可以考虑使用一些内置数据库支持,例如 核心数据。
While @Robin's answer is spot on, you might also consider using some of the built-in database support, like Core Data.
好吧,搜索大量数据总是很困难,尤其是在 iPhone/iPod 上。在这种情况下,我认为您应该做的只是允许用户根据这些条件之一进行搜索,并简单地搜索包含该数据的各个数组。这些数组之一的示例可以是
数组
matt
bob
mrlol
/array
然后使用该数组中的索引号并提取有问题的字典。这可能不是最清楚的解释,如果您有疑问,请评论。
编辑:对于之前的答案,我不认为他是在问如何比较数据,我认为他想要一种搜索数据的方法,所以我不认为 NSPredicate 是他正在寻找的东西。
Ok, searching through large amounts of data is always going to be tough, especially on an iPhone/iPod. In this case, what i think you should do is only allow the user to search based on one of those criteria, and simply search through individual arrays containing that data. An example of one of those arrays could be
array
matt
bob
mrlol
/array
Then use the index number from that array and extract the dictionary in question. This may not have been the most clear explanation, please comment if you have questions.
Edit: To the previous answer, i don't think he was asking about how to compare data, i think he wanted a way to search through data, so i don't think NSPredicate was what he was looking for.