I have an NSString object and want to convert it into a std::string.
How do I do this in Objective-C++?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Edit: After a few years, let me expand on this answer. As rightfully pointed out, you’ll most likely want to use
cStringUsingEncoding:withNSASCIIStringEncodingif you are going to end up usingstd::string. You can use UTF-8 with normalstd::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.Also note, if you have a string that can’t be represented as ASCII but you still want it in an
std::stringand you don’t want non-ASCII characters in there, you can usedataUsingEncoding:allowLossyConversion:to get anNSDatarepresentation of the string with lossy encoded ASCII content, and then throw that at yourstd::string