Convert a CFStringRef to a C string?

6.7k views Asked by At

Hello lovely computer people:

I would like to convert the following CFStringRef into a CString. Any idea how?

recordFilePath = (CFStringRef)[NSTemporaryDirectory() stringByAppendingPathComponent: @"recordedFile.wav"];

Thanks!


EDIT 1

DarkDust answer seems to come close, but I'm still getting an error (see comment). Any help?

2

There are 2 answers

5
DarkDust On BEST ANSWER

Since a CFStringRef can be toll-free casted to NSString, you can simply do:

myCString = [(NSString *)myCFStringRef UTF8String];

or in your case:

myCString = [[NSTemporaryDirectory() stringByAppendingPathComponent: @"recordedFile.wav"] UTF8String];
0
Vikram Singh On

A straightforward solution:

CFStringGetCString(myCFStringRef, myCStringPointer, mySize, myEncoding);

Checkout this function:

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