NSString *foo = @"Foo"; std::string bar = std::string([foo UTF8String]);
Edit: in a few years, let me expand on this answer. As rightly pointed out, you will most likely want to use cStringUsingEncoding: with NSASCIIStringEncoding if you intend to use std::string . You can use UTF-8 with regular std::strings , but keep in mind that they work with bytes, not characters or even graphemes. For a good βget startedβ check out this question and its answer .
Also note: if you have a string that cannot be represented as ASCII, but you still want it in std::string and you don't need non-ASCII characters, you can use dataUsingEncoding:allowLossyConversion: for retrieve a NSData representation of a string with ASCII content with lossy encoding, and then throw it into your std::string
JustSid Nov 03 2018-11-11T00: 00Z
source share