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

时间:2022-09-07 08:25:44

I have an NSString object and want to convert it into a std::string.

我有一个NSString对象,想把它转换成std::string。

How do I do this in Objective-C++?

我如何在objective - c++中做到这一点?

3 个解决方案

#1


106  

NSString *foo = @"Foo";
std::string bar = std::string([foo UTF8String]);

Edit: After a few years, let me expand on this answer. As rightfully pointed out, you'll most likely want to use cStringUsingEncoding: with NSASCIIStringEncoding if you are going to end up using std::string. You can use UTF-8 with normal std::strings, but keep in mind that those operate on bytes and not on characters or even graphemes. For a good "getting started", check out this question and its answer.

编辑:几年之后,让我把这个答案扩展一下。正确地指出,您很可能想要使用cStringUsingEncoding:如果您最终使用std::string,则使用NSASCIIStringEncoding。您可以使用标准的std::字符串使用UTF-8,但是要记住,这些操作是基于字节而不是字符,甚至是graphemes。为了一个好的“开始”,看看这个问题和它的答案。

Also note, if you have a string that can't be represented as ASCII but you still want it in an std::string and you don't want non-ASCII characters in there, you can use dataUsingEncoding:allowLossyConversion: to get an NSData representation of the string with lossy encoded ASCII content, and then throw that at your std::string

还要注意,如果你有一个不能被表示成ASCII字符串但你仍然想要它在std::string和你不想要非ASCII字符,您可以使用dataUsingEncoding:allowLossyConversion:得到一个NSData表示有损ASCII编码的字符串内容,然后扔在你的std::string

#2


48  

As Ynau's suggested in the comment, in a general case it would be better to keep everything on the stack instead of heap (using new creates the string on the heap), hence (assuming UTF8 encoding):

正如Ynau在评论中建议的那样,在一般情况下,最好把所有东西都放在堆栈上而不是堆上(使用new在堆上创建字符串),因此(假设UTF8编码):

NSString *foo = @"Foo";
std::string bar([foo UTF8String]);

#3


1  

As noted on philjordan.eu it could also be that the NSString is nil. In such a case the cast should be done like this:

philjordan如上所述。eu也可能是NSString是nil。在这种情况下,演员应该这样做:

// NOTE: if foo is nil this will produce an empty C++ string

//注意:如果foo是nil,这将产生一个空的c++字符串。

// instead of dereferencing the NULL pointer from UTF8String.

//而不是从UTF8String删除空指针。

This would lead you to such a conversion:

这将引导您进行这样的转换:

NSString *foo = @"Foo";
std::string bar = std::string([foo UTF8String], [foo lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);

#1


106  

NSString *foo = @"Foo";
std::string bar = std::string([foo UTF8String]);

Edit: After a few years, let me expand on this answer. As rightfully pointed out, you'll most likely want to use cStringUsingEncoding: with NSASCIIStringEncoding if you are going to end up using std::string. You can use UTF-8 with normal std::strings, but keep in mind that those operate on bytes and not on characters or even graphemes. For a good "getting started", check out this question and its answer.

编辑:几年之后,让我把这个答案扩展一下。正确地指出,您很可能想要使用cStringUsingEncoding:如果您最终使用std::string,则使用NSASCIIStringEncoding。您可以使用标准的std::字符串使用UTF-8,但是要记住,这些操作是基于字节而不是字符,甚至是graphemes。为了一个好的“开始”,看看这个问题和它的答案。

Also note, if you have a string that can't be represented as ASCII but you still want it in an std::string and you don't want non-ASCII characters in there, you can use dataUsingEncoding:allowLossyConversion: to get an NSData representation of the string with lossy encoded ASCII content, and then throw that at your std::string

还要注意,如果你有一个不能被表示成ASCII字符串但你仍然想要它在std::string和你不想要非ASCII字符,您可以使用dataUsingEncoding:allowLossyConversion:得到一个NSData表示有损ASCII编码的字符串内容,然后扔在你的std::string

#2


48  

As Ynau's suggested in the comment, in a general case it would be better to keep everything on the stack instead of heap (using new creates the string on the heap), hence (assuming UTF8 encoding):

正如Ynau在评论中建议的那样,在一般情况下,最好把所有东西都放在堆栈上而不是堆上(使用new在堆上创建字符串),因此(假设UTF8编码):

NSString *foo = @"Foo";
std::string bar([foo UTF8String]);

#3


1  

As noted on philjordan.eu it could also be that the NSString is nil. In such a case the cast should be done like this:

philjordan如上所述。eu也可能是NSString是nil。在这种情况下,演员应该这样做:

// NOTE: if foo is nil this will produce an empty C++ string

//注意:如果foo是nil,这将产生一个空的c++字符串。

// instead of dereferencing the NULL pointer from UTF8String.

//而不是从UTF8String删除空指针。

This would lead you to such a conversion:

这将引导您进行这样的转换:

NSString *foo = @"Foo";
std::string bar = std::string([foo UTF8String], [foo lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);