什么时候在 NSArray 上使用 NSSet 更好? [英] When is it better to use an NSSet over an NSArray?

查看:33
本文介绍了什么时候在 NSArray 上使用 NSSet 更好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中多次使用 NSSet,但我自己从未创建过.

I have used NSSets many times in my apps, but I have never created one myself.

什么时候使用 NSSet 而不是 NSArray 为什么?

When is it better to use an NSSet as opposed to an NSArray and why?

推荐答案

当集合中项目的顺序不重要时,集合为查找集合中的项目提供更好的性能.

When the order of the items in the collection is not important, sets offer better performance for finding items in the collection.

原因是集合使用哈希值来查找项目(如字典),而数组必须遍历其整个内容才能找到特定对象.

The reason is that a set uses hash values to find items (like a dictionary) while an array has to iterate over its entire contents to find a particular object.

这篇关于什么时候在 NSArray 上使用 NSSet 更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆