如何掌握 NSMutableDictionary、NSEnumerator、NSMutableSet?
我对这三个主题的知识有点落后:NSMutableDictionary
、NSEnumerator
、NSMutableSet
。当我想要使用它们时,感觉非常困难,即使我已经阅读了开发人员文档。
是否有任何示例代码可以清楚地理解这三个主题?
请帮我。
谢谢你, 马丹·莫汉。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
理解这些的最佳方法取决于您之前的经验。
NSDictionary
顾名思义:一本字典。这意味着给定一个键(或一个中心词,如字典中的),您可以查找一个值(或定义):例如,该字典给出关于我的狗的信息:
使用包含该信息的字典
dogInfo
,我们可以发送[dogInfo objectForKey:@"name"]
并期望收到@"tucker"
。NSDictionary
和NSMutableDictionary
之间的区别在于后者允许在初始化后进行更改。这使您可以执行诸如[dogInfo setObject:@"fetch" forKey:@"game"]
之类的操作。这对于维护状态、记忆引用透明请求等很有帮助。NSSet
是一种拥有一堆对象的方法,有一些重要的位:这些对象没有定义的顺序,并且可以只能是每个对象之一(无重复)。当您需要包含唯一的、无序的对象时,请使用 NSSet。NSMutableSet
是NSSet
的变体,它允许在初始化后进行更改(例如添加或删除对象)。NSEnumerator
有点复杂,但您通常不需要处理它,除非您正在编写自己的库、正在编写古老的代码或正在执行复杂的枚举。NSEnumerator
的子类由集合类(例如NSDictionary
、NSArray
和NSSet
)使用,以允许其对象来一一列举。通常,您只需使用foreach
循环枚举它们,因为它们都实现
。但有时,您会想要执行更具体的操作,例如枚举字典的对象(而不是键),或反向枚举数组。这就是NSEnumerator
的实例(通常定义为集合对象的属性)将变得有用的地方。更新
Justin 在评论中指出
NSEnumerator
符合
;这意味着您几乎不需要知道如何使用NSEnumerator
;您可以对枚举器本身执行foreach
循环,如下所示: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:
With a dictionary
dogInfo
containing that information, we could send[dogInfo objectForKey:@"name"]
and expect to receive@"tucker"
.The difference between
NSDictionary
andNSMutableDictionary
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). UseNSSet
for when you need to contain unique, unordered objects.NSMutableSet
is the variant ofNSSet
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 ofNSEnumerator
are used by collection classes, such asNSDictionary
,NSArray
, andNSSet
, to allow their objects to be enumerated. Usually, you'd just enumerate over them using aforeach
-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 ofNSEnumerator
(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 anNSEnumerator
; you can just do aforeach
loop over the enumerator itself, like so: