什么是 NSCF 词典?
我收到一个 NSCFDictionary 返回给我,但我不知道如何使用它。我知道它是 NSCFDictionary 类型,因为我打印了该类,结果显示为 __NCSFDictionary。我不知道如何用它做任何事情。
我现在只是想保留它,但甚至无法让它发挥作用:
NSDictionary *dict = [[NSURLCredentialStorage sharedCredentialStorage] allCredentials];
for(NSURLProtectionSpace key in [dict keyEnumerator])
{
NSCFDictionary *value = [dict objectForKey:key];
}
allCredentials 的类引用说它应该返回一个字典,其值也是字典。但我的作业声明不起作用。我需要某种演员吗?
I'm getting an NSCFDictionary returned to me and I can't figure out how to use it. I know it's of type NSCFDictionary because I printed the class and it came out as __NCSFDictionary. I can't figure out how to do anything with it.
I'm just trying to hold onto it for now but can't even get that to work:
NSDictionary *dict = [[NSURLCredentialStorage sharedCredentialStorage] allCredentials];
for(NSURLProtectionSpace key in [dict keyEnumerator])
{
NSCFDictionary *value = [dict objectForKey:key];
}
The class reference for allCredentials says its supposed to return a dictionary whose values are also dictionaries. My assignment statement isn't working though. Do I need a cast of some kind?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
NSDictionary 和其他 集合类实际上是类簇:几个具体子类伪装的类在单个类的接口下:它们都提供相同的功能(因为它们是同一类的子类 - 在 NSDictionary 的情况下,这涉及三个“原始方法”
-count、
-objectForKey:
和-keyEnumerator
),但根据它们的创建方式和数据类型,它们具有不同的内部工作方式,以便在不同情况下保持高效他们可能正在存储。NSCFDictionary 只是 NSDictionary 的具体子类。 也就是说,您的 NSDictionaries 实际上可能是 NSCFDictionary 实例,但您应该将它们视为 NSDictionary 的实例,因为这将为您提供所需的字典存储功能。
现在,您的代码不起作用的另一个原因是: NSURLProtectionSpace 是一个类,因此您应该将其用作指针,如下所示:
NSDictionary and the other collection classes are actually class clusters: several concrete subclasses classes masquerading under the interface of a single class: they all provide the same functionality (because they are subclasses of the same class — in NSDictionary's case, this involves the three "primitive methods"
-count
,-objectForKey:
, and-keyEnumerator
), but have different internal workings to be efficient in different situations, based on how they're created and what type of data they may be storing.NSCFDictionary is simply a concrete subclass of NSDictionary. That is, your NSDictionaries may actually be NSCFDictionary instances, but you should treat them as instances of NSDictionary, because that will provide you with the required dictionary-storage functionality.
Now, another reason your code doesn't work: NSURLProtectionSpace is a class, so you should use it as a pointer, like this:
NSCFDictionary 是 NSDictionary 的私有子类,它实现了实际的功能。它只是一个 NSDictionary。几乎您使用的任何 NSDictionary 都将是底层的 NSCFDictionary。这对你的代码来说并不重要。您可以将变量键入 NSDictionary 并相应地使用它。
NSCFDictionary is the private subclass of NSDictionary that implements the actual functionality. It's just an NSDictionary. Just about any NSDictionary you use will be an NSCFDictionary under the hood. It doesn't matter to you code. You can type the variable as NSDictionary and use it accordingly.
我有一个 NSCFDictionary,它实际上是一个 NSMutableDictionary 对象,我可以从中删除项目。我提到这一点是为了进一步澄清 jtbandes 的答案:NSCFDictionary 对象可以是继承自 NSDictionary 的任何对象。
I have an NSCFDictionary that is actually a NSMutableDictionary object, I can delete items from it. I mention this to further clarify jtbandes' answer: the NSCFDictionary object may be any object that inherits from NSDictionary.