简体   繁体   English

如何将存储在CFArrayRef中的CFStringRef值检索到strings / char *数组

[英]How to retrieve CFStringRef values stored in CFArrayRef to an array of strings / char *

I am new to cross platform programming. 我是跨平台编程的新手。 I am facing an a problem here, but because of very limited online help ( or may be i do not know where to search ) I could not find a solution to it. 我在这里面临一个问题,但由于在线帮助非常有限(或者我可能不知道在哪里搜索),我无法找到解决方案。

The problem is: I want to convert a CFArrayRef to a usable format in C++/CI have a CFArrayRef which holds language codes in CFStringRef format. 问题是:我想将CFArrayRef转换为C ++ / CI中的可用格式,其CFArrayRef保存CFStringRef格式的语言代码。 I want to retrieve it in some std::vector or C++ array of char* 我想在一些std :: vector或C ++的char *数组中检索它

Say you have a CFArrayRef of some CFStringRef, then all you have to do is to count the number of elements of vector, Iterate on the CFArrayRef, get the string out of it and do whatever you want to do with it, Here in sample code for an instance I am extracting elements of CFArrayRef and storing them in std::vector of std::strings. 假设您有一些CFStringRef的CFArrayRef,那么您所要做的就是计算向量的元素数量,迭代CFArrayRef,从中获取字符串并做任何您想要做的事情,在示例代码中对于一个实例,我正在提取CFArrayRef的元素并将它们存储在std :: vector的std :: strings中。

Say here "names" is a CFArrayRef containing some names: 在这里说“names”是一个包含一些名字的CFArrayRef:

// Count of available names in ArrayRef
CFIndex nameCount = CFArrayGetCount( names );

std::vector<std::string> vecNames;

//Iterate through the CFArrayRef and fill the vector
for( int i = 0; i < nameCount ; ++i  )
{
    CFStringRef sName = (CFStringRef)CFArrayGetValueAtIndex( names, i );
    strName = CFStringGetCStringPtr( sName , kCFStringEncodingMacRoman );
    vecNames.push_back( strName );
}

CFString has this: CFStringGetCString CFString有这个: CFStringGetCString

Copies the character contents of a string to a local C string buffer after converting the characters to a given encoding. 将字符转换为给定编码后,将字符串的字符内容复制到本地C字符串缓冲区。

Boolean CFStringGetCString (
   CFStringRef theString,
   char *buffer,
   CFIndex bufferSize,
   CFStringEncoding encoding
);

Parameters: theString 参数: theString

The string whose contents you wish to access. 您要访问其内容的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM