Search code examples
objective-cunichar

char, wchar_t and unichar


I'm writing my first ever Objective-C/Cocoa program. (I already know C and C++ in the Windows world, i.e. Visual C++.) It looks like Cocoa can work with the char type, but prefers the unichar type. It also looks like Cocoa does NOT much care for wchar_t. So what type should I use for the C portion of my code, e.g. in structs? If the answer is unichar then what are the equivalents of const wchar_t* myString = L"this is my wchar string";, wcslcpy, etc.?


Solution

  • Besides unfamiliarity, is there any reason why you aren't talking about NSStrings?

    NSStrings are the standard string types in Objective C. Unless you're trying to build some cross compiling C++ library into an iOS / Macintosh app, there probably isn't much reason to cling to a C-type.

    Here's a related question that also includes further information and somebody's super useful looking set of methods that convert wchar_t into a NSString object and vice versa.