Created
November 6, 2013 14:48
-
-
Save dodikk/7337210 to your computer and use it in GitHub Desktop.
Converts NSString to std::string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (std::string)toStlString | |
{ | |
if (nil == self) | |
{ | |
return ""; | |
} | |
NSUInteger bytesSize = [self lengthOfBytesUsingEncoding: NSUTF8StringEncoding] + sizeof( char ); | |
std::vector<char> bytesVt( bytesSize, 0x00 ); | |
BOOL getBytesResult = | |
[ self getCString: &bytesVt.at( 0 ) | |
maxLength: bytesSize | |
encoding: NSUTF8StringEncoding ]; | |
if ( !getBytesResult ) | |
{ | |
NSLog(@"[toStlString] : not enough memory reserved."); | |
return ""; | |
} | |
std::string urlCppString | |
( | |
bytesVt.begin(), bytesVt.end() | |
); | |
return urlCppString; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This works better