2

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.?

HairOfTheDog
  • 2,489
  • 2
  • 29
  • 35
  • 1
    There's nothing to stop you using NSStrings in structs – Dave Mar 07 '13 at 01:26
  • (but don't forget to call `release` when you're done with it!) – Dave Mar 07 '13 at 01:27
  • @Dave actually, in ARC there is (compiler warnings and the such). If he's using a C++ struct, then there's no problem though. – Richard J. Ross III Mar 07 '13 at 01:42
  • @RichardJ.RossIII because ARC can't guarantee the memory management for it. You can use __unsafe_unretained objects in structs freely. – CodaFi Mar 07 '13 at 01:45
  • ah ok. I've only ever used Objective C with C++, and assumed the same principles applied to C. – Dave Mar 07 '13 at 01:47
  • @Dave the need to use special syntax and the need to call `release` are the very reasons I do not want to use NSString in a struct. I want to allocate the struct with `malloc` and deallocate the struct with `free` – HairOfTheDog Mar 07 '13 at 01:50

1 Answers1

2

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.

Community
  • 1
  • 1
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215