Garifield
Garifield

Reputation: 61

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++/C I have a CFArrayRef which holds language codes in CFStringRef format. I want to retrieve it in some std::vector or C++ array of char*

Upvotes: 3

Views: 5323

Answers (2)

PRIME
PRIME

Reputation: 1078

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.

Say here "names" is a CFArrayRef containing some names:

// 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 );
}

Upvotes: 10

uchuugaka
uchuugaka

Reputation: 12782

CFString has this: CFStringGetCString

Copies the character contents of a string to a local C string buffer after converting the characters to a given encoding.

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

Parameters: theString

The string whose contents you wish to access.

Upvotes: 2

Related Questions