xcode 从 CFDictionary 中获取值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3195203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:31:18  来源:igfitidea点击:

Getting values from a CFDictionary

objective-ccocoaxcodemacos

提问by Eric Brotto

I'm trying to access the values from a CFDictionary. I've started by implementing code suggested in this question:

我正在尝试从 CFDictionary 访问值。我已经开始实施这个问题中建议的代码:

 CFTypeRef r = IOPSCopyPowerSourcesInfo();
 CFArrayRef array = IOPSCopyPowerSourcesList(r);
 CFDictionaryRef powerDic = IOPSGetPowerSourceDescription(array, r);

I have looked at the documentation plus other posts, but it's a bit beyond me how to work it.

我已经查看了文档和其他帖子,但它有点超出我的工作范围。

What I really need is some example code that takes the code I already have and uses it to print a string of, for example, "Current Capacity".

我真正需要的是一些示例代码,它采用我已有的代码并使用它来打印一个字符串,例如“当前容量”。

回答by Dave DeLong

CFDictionaryRefis "toll free bridged" with NSDictionary. This means you can cast one to the other interchangeably. In other words:

CFDictionaryRefNSDictionary. 这意味着您可以交替地将一个转换为另一个。换句话说:

...
NSDictionary * powerDic = (NSDictionary *)IOPSGetPowerSourceDescription(array, r);
NSString * aValue = [powerDic objectForKey:@"aKey"];

If (for some reason) you need to stay at the CoreFoundation level (which isn't unheard-of), you'd use something like CFDictionaryGetValue().

如果(出于某种原因)您需要保持在 CoreFoundation 级别(这并非闻所未闻),您可以使用类似CFDictionaryGetValue().