如何掌握 NSMutableDictionary、NSEnumerator、NSMutableSet?

发布于 2024-10-08 09:15:46 字数 203 浏览 3 评论 0原文

我对这三个主题的知识有点落后:NSMutableDictionaryNSEnumeratorNSMutableSet。当我想要使用它们时,感觉非常困难,即使我已经阅读了开发人员文档。

是否有任何示例代码可以清楚地理解这三个主题?

请帮我。

谢谢你, 马丹·莫汉。

I am little bit backward in knowledge on these three topics: NSMutableDictionary, NSEnumerator, NSMutableSet. When I want to use them, it feels very hard, even when I've gone through the developer documentation.

Is there any sample code to understand it clearly for all three topics?

Please help me.

Thank you,
Madan Mohan.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尽揽少女心 2024-10-15 09:15:46

理解这些的最佳方法取决于您之前的经验。 NSDictionary 顾名思义:一本字典。这意味着给定一个键(或一个中心词,如字典中的),您可以查找一个值(或定义):

例如,该字典给出关于我的狗的信息:

KEY        VALUE
-------------------------------------------
@"name"       @"tucker"
@"species"    @"canis lupus familiaris"
@"favorite"   @"biscuits"

使用包含该信息的字典 dogInfo,我们可以发送 [dogInfo objectForKey:@"name"] 并期望收到 @"tucker"

NSDictionaryNSMutableDictionary 之间的区别在于后者允许在初始化后进行更改。这使您可以执行诸如 [dogInfo setObject:@"fetch" forKey:@"game"] 之类的操作。这对于维护状态、记忆引用透明请求等很有帮助。

NSSet 是一种拥有一堆对象的方法,有一些重要的位:这些对象没有定义的顺序,并且可以只能是每个对象之一(无重复)。当您需要包含唯一的、无序的对象时,请使用 NSSet。 NSMutableSetNSSet 的变体,它允许在初始化后进行更改(例如添加或删除对象)。

NSEnumerator 有点复杂,但您通常不需要处理它,除非您正在编写自己的库、正在编写古老的代码或正在执行复杂的枚举。 NSEnumerator 的子类由集合类(例如 NSDictionaryNSArrayNSSet)使用,以允许其对象来一一列举。通常,您只需使用 foreach 循环枚举它们,因为它们都实现 。但有时,您会想要执行更具体的操作,例如枚举字典的对象(而不是键),或反向枚举数组。这就是 NSEnumerator 的实例(通常定义为集合对象的属性)将变得有用的地方。

更新

Justin 在评论中指出 NSEnumerator 符合 ;这意味着您几乎不需要知道如何使用 NSEnumerator;您可以对枚举器本身执行 foreach 循环,如下所示:

for (id object in [dogDict objectEnumerator]) {
  // doing something with the object, disregarding its key
}

The best way to understand these depends on what your prior experience is. NSDictionary is exactly what it sounds like: a dictionary. What that means is that given a key (or a headword, as in a dictionary), you can look up a value (or definition):

For instance, this dictionary gives information about my dog:

KEY        VALUE
-------------------------------------------
@"name"       @"tucker"
@"species"    @"canis lupus familiaris"
@"favorite"   @"biscuits"

With a dictionary dogInfo containing that information, we could send [dogInfo objectForKey:@"name"] and expect to receive @"tucker".

The difference between NSDictionary and NSMutableDictionary is that the latter allows changes after initialization. This lets you do things like [dogInfo setObject:@"fetch" forKey:@"game"]. This is helpful for maintaining state, memoizing referentially transparent requests, etc.

NSSet is a way to have a bunch of objects, with a few important bits: there is no defined order to those objects, and there can only be one of each object (no duplicates). Use NSSet for when you need to contain unique, unordered objects. NSMutableSet is the variant of NSSet which allows for changes (such as adding or removing objects) after initialization.

NSEnumerator is a bit more complicated, but you usually won't need to deal with it unless you are writing your own libraries, are coding archaically, or are doing complex enumerations. Subclasses of NSEnumerator are used by collection classes, such as NSDictionary, NSArray, and NSSet, to allow their objects to be enumerated. Usually, you'd just enumerate over them using a foreach-loop, since they all implement <NSFastEnumeration>. But sometimes, you'll want to do more specific things, like enumerate over the objects (instead of the keys) of a dictionary, or enumerate over an array in reverse. This is where instances of NSEnumerator (usually defined as properties on your collection objects) will become helpful.

Update

Justin in the comments pointed out that NSEnumerator conforms to <NSFastEnumeration>; that means, the chances are next-to-nothing that you'll need to know how to use an NSEnumerator; you can just do a foreach loop over the enumerator itself, like so:

for (id object in [dogDict objectEnumerator]) {
  // doing something with the object, disregarding its key
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文