I am trying to get the current user name in c++ using the NSFullUserName function but it returns an NSString. Therefore, how can I convert an NSString to a CString in c++ without Obj-C code?
I am trying to get the current user name in c++ using the NSFullUserName
Share
NSStringis toll-free bridged withCFString(ie, you can just cast the pointer — the thing it points to can be interpreted as either type), and you can manipulateCFStringusing vanilla C calls.So you’d typically attempt
CFStringGetCStringPtrto try to get a pointer directly to theNSStringcontents as a C string without any copying, then fall back on allocating a suitably sized C-style array andCFStringGetCStringif the direct pointer isn’t available (typically because the encoding you want doesn’t match theNSString‘s internal encoding).CFStringGetMaximumSizeForEncodingwill return the maximum size of buffer you need to allocate for a given encoding and string length,CFStringGetLengththe length of a given string.So e.g.