运维开发网

详解NSString 与C++ string字符串的互转

运维开发网 https://www.qedev.com 2020-04-01 16:30 出处:网络 作者: typename
NSString 与C++ string字符串的互转实例详解 1.  string 转换为 NSString  std::string str(\"hello\");

NSString 与C++ string字符串的互转实例详解

1.  string 转换为 NSString 

std::string str("hello"); 
NSString *str=[NSString stringWithString:str.c_str()]; 
 
NSString *istr=[NSString stringWithString:@"zsh"]; 
 
 str=[istr cStringUsingEncoding: NSUTF8StringEncoding]; 
 NSString *path= [[NSBundle mainBundle]pathForResource:@"nokia" ofType:@"mp3"]; 
 
 char *tempPath=(char*)malloc(sizeof(path)+1); 
  
 NSString *soundPath=[[NSBundle mainBundle] pathForResource:@mp3"];  
  
 NSString* dirStr = [NSString stringWithFormat:@"%s",path]; 
 NSString* filepath = [[NSBundle mainBundle] pathForResource:dirStr ofType:nil]; 
 NSLog(filepath); 

 2.  //NSString 转换为C 字符串

const char* destDir = [filepath UTF8String]; 
 
/ NSURL *soundUrl=[[NSURL alloc] initFileURLWithPath:s];  

3.const char * to NSString

char *str ="abc"; 
SString *s = [NSString stringWithUTF8String:str];

以上就是NSString 与C++ string字符串的互转的实现代码,如有疑问请留言或者到社区讨论,谢 谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

扫码领视频副本.gif

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号