2
+(const char /*wchar_t*/ *)wcharFromString:(NSString *)string
{
    return  [string cStringUsingEncoding:NSUTF8StringEncoding];
}

Does it return char or wchar_t?

from the method name, it should return wchar_t, but why there is a comment around wchar_t return type?

Source is here:

How to convert wchar_t to NSString?

Community
  • 1
  • 1
Adam Lee
  • 24,710
  • 51
  • 156
  • 236

2 Answers2

3

That code just looks incorrect. They're claiming it does one thing, but it actually does another. The return type is const char *.

Catfish_Man
  • 41,261
  • 11
  • 67
  • 84
1

This method is not correct. It returns a const char *, encoded as a UTF8 string. That is a perfectly sensible way of getting a C string from an NSString, but nowhere here is anyone actually doing anything with wchar_ts.

wchar_t is a "wide char", and a pointer to it would be a "wide string" (represented by const wchar_t *). These are designed to precisely represent larger character sets, and can be two-byte wide character strings; they use a whole different variant set of string manipulation functions to do things with them. (Strings like this are very rarely seen in iOS development, for what it's worth.)

Ben Zotto
  • 70,108
  • 23
  • 141
  • 204