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

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字符串的互转的实现代码,如有疑问请留言或者到社区讨论,谢 谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

相关文章

发表回复

您的电子邮箱地址不会被公开。