SoFunction
Updated on 2025-04-05

Detailed explanation of the interchange between NSString and C++ string strings

Detailed explanation of the inter-transfer instance between NSString and C++ string strings

1. Convert string to 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. // Convert NSString to C string

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

char * to NSString

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

The above is the implementation code for the interchange of NSString and C++ string strings. If you have any questions, please leave a message or go to the community to discuss. Thank you for reading. I hope it can help you. Thank you for your support for this site!