Objective C 数组“key”而不是索引
我希望能够使用 NSString 或类似的东西来存储字符串数组,但通过键(我认为这就是它的名字)而不是数字索引来访问它们。
例如,不要这样做:
return names[3];
做这样的事情:
return names["Bob"];
如何存储和检索这样的值?
请原谅我对此缺乏了解。我使用 Objective C 已经有大约一年的时间了,但从来没有做过这样的事情。
I want to be able to use NSString
, or something similar to store an array of strings, but access them by a key (I think this is what it's called), instead of a numeric index.
For example, instead of doing this:
return names[3];
Do something like this:
return names["Bob"];
How do you store and retrieve values like this?
Please excuse my lack of knowledge of this. I've been working with Objective C for about a year now, but never had to do anything like this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 Objective-C 中,您可以使用
NSDictionary
类及其可变子类NSMutableDictionary
来完成此操作。您可以使用以下方式获取键的值In Objective-C, you do this with the
NSDictionary
class and its mutable subclassNSMutableDictionary
. You can get the value for a key usingNSDictionary
NSDictionary
这些类型的数组取自
C
(其中Objective-C
是超集),仅允许整数作为索引。您可能希望使用
NSDictionary
及其objectWithKey
样式消息。Those kind of arrays are taken from
C
(of whichObjective-C
is a superset) which only allow integers as their indices.You may wish to use
NSDictionary
and it'sobjectWithKey
style messages.