如何将 std: : string 转换为 NSString?

我试图将一个标准的 std::string转换成一个 NSString,但我没有太多的运气。

我可以用以下代码成功地将 NSString转换为 std::string

NSString *realm = @"Hollywood";
std::string REALM = [realm cStringUsingEncoding:[NSString defaultCStringEncoding]];

然而,当我尝试下面的操作时,我得到了一个编译时间错误

NSString *errorMessage = [NSString stringWithCString:REALM encoding:[NSString defaultCStringEncoding]];

我得到的错误是

Cannot convert 'std::string' to 'const char*' in argument passing

我错过了什么吗?

89484 次浏览

Get c-string out of std::string for conversion:

NSString *errorMessage = [NSString stringWithCString:REALM.c_str()
encoding:[NSString defaultCStringEncoding]];

Firstly, you've got to be using Objective-C++ for this to work in the slightest; easiest way to ensure that is rename all your *.m files to *.mm

By far the most usable (non-deprecated) manual way of getting a C++ std::string into an NSString is with:

std::string param; // <-- input
NSString* result = [NSString stringWithUTF8String:param.c_str()];
NSString* alternative = [[NSString alloc] initWithUTF8String:param.c_str()];

This will work in most cases - and if you're not doing specific encoding detection and conversion, UTF-8 is going to give you a good result for having non-latin characters 'just work.'

If you're making a bigger app, or you're not the only one working on it, however - you'll probably want something that's easier to apply.

Adapted from cocoa-dev mailing list archives

@interface NSString (cppstring_additions)
+(NSString*) stringWithwstring:(const std::wstring&)string;
+(NSString*) stringWithstring:(const std::string&)string;
-(std::wstring) getwstring;
-(std::string) getstring;
@end


@implementation NSString (cppstring_additions)


#if TARGET_RT_BIG_ENDIAN
const NSStringEncoding kEncoding_wchar_t = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingUTF32BE);
#else
const NSStringEncoding kEncoding_wchar_t = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingUTF32LE);
#endif


+(NSString*) stringWithwstring:(const std::wstring&)ws
{
char* data = (char*)ws.data();
unsigned size = ws.size() * sizeof(wchar_t);


NSString* result = [[NSString alloc] initWithBytes:data length:size encoding:kEncoding_wchar_t];
return result;
}
+(NSString*) stringWithstring:(const std::string&)s
{
NSString* result = [[NSString alloc] initWithUTF8String:s.c_str()];
return result;
}


-(std::wstring) getwstring
{
NSData* asData = [self dataUsingEncoding:kEncoding_wchar_t];
return std::wstring((wchar_t*)[asData bytes], [asData length] / sizeof(wchar_t));
}
-(std::string) getstring
{
return [self UTF8String];
}


@end

With that in-place (and appropriately #imported) you can now:

NSString* result = [NSString stringWithstring:param];
string convertedBack = [result getstring];

And the same for std::wstring, which is more than handy.

I've also found that:

NSString *nsString = [NSString stringWithFormat:@"%s",standardString];

Works like a champ.

NSString* mystring = [NSString stringWithUTF8String:stdstring.c_str()];

Apple now has a new way they want you to do this conversion. In XCode7, I used the Edit > Convert > To Modern Objective C Syntax... option to find this out. It uses a shorthand @ symbol.

std::string sCPPString = "Hello World!";
NSString *sAppleString = @(sCPPString.c_str());

Here is the code snippet/example:

string str_simple = "HELLO WORLD";


//string to NSString
NSString *stringinObjC = [NSString stringWithCString:str_simple.c_str()
encoding:[NSString defaultCStringEncoding]];
NSLog(stringinObjC);