Hi I am trying to convert a standard std::string
into an NSString
but I'm not having much luck.
I can convert successfully from an NSString
to a std::string
with the following code
NSString *realm = @"Hollywood"; std::string REALM = [realm cStringUsingEncoding:[NSString defaultCStringEncoding]];
However I get a compile time error when I try the following
NSString *errorMessage = [NSString stringWithCString:REALM encoding:[NSString defaultCStringEncoding]];
The error I get is
Cannot convert 'std::string' to 'const char*' in argument passing
Am I missing something here?
Thanks in advance.
std::wstring stemp = std::wstring(s. begin(), s. end()); LPCWSTR sw = stemp. c_str();
Working with NSString. Instances of the class NSString are immutable – their contents cannot be changed. Once a string has been initialized using NSString, the only way to append text to the string is to create a new NSString object. While doing so, you can append string constants, NSString objects, and other values.
A static, plain-text Unicode string object that bridges to String ; use NSString when you need reference semantics or other Foundation-specific behavior.
Get c-string out of std::string for conversion:
NSString *errorMessage = [NSString stringWithCString:REALM.c_str() encoding:[NSString defaultCStringEncoding]];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With