详解NSString 与C++ string字符串的互转
NSString与C++string字符串的互转实例详解
1. string转换为NSString
std::stringstr("hello"); NSString*str=[NSStringstringWithString:str.c_str()]; NSString*istr=[NSStringstringWithString:@"zsh"]; str=[istrcStringUsingEncoding:NSUTF8StringEncoding]; NSString*path=[[NSBundlemainBundle]pathForResource:@"nokia"ofType:@"mp3"]; char*tempPath=(char*)malloc(sizeof(path)+1); NSString*soundPath=[[NSBundlemainBundle]pathForResource:@mp3"]; NSString*dirStr=[NSStringstringWithFormat:@"%s",path]; NSString*filepath=[[NSBundlemainBundle]pathForResource:dirStrofType:nil]; NSLog(filepath);
2. //NSString转换为C字符串
constchar*destDir=[filepathUTF8String]; /NSURL*soundUrl=[[NSURLalloc]initFileURLWithPath:s];
3.constchar*toNSString
char*str="abc"; SString*s=[NSStringstringWithUTF8String:str];
以上就是NSString与C++string字符串的互转的实现代码,如有疑问请留言或者到社区讨论,谢谢阅读,希望能帮助到大家,谢谢大家对本站的支持!