web123456

char *, char, string and NSString conversion (objec-c and c++ mixing basics)

Strings are the most important elements that connect object-c and c++. Learning the processing of strings is equivalent to learning the mixing of oc and c++



char * /const char *andConversion between NSStrings


//char * /const char * changeNSString

NSString * strPath = [NSString stringWithUTF8String:filename];


//NSStringchangechar * /const char * 

const char * filePathChar = [filePath UTF8String];

////Gorgeous dividing line //

Convert char to nsstring

char myChar = 'a';
NSString * string = [NSString stringWithFormat:@"%c", myChar];
Extract a field of NSString tochar

- (unichar)characterAtIndex:(NSUInteger)index;


// // Gorgeous dividing line //


C++ and oc themselves cannot be directly connected. To connect through the c API.

string to NSString

string str = [aNSString UTF8String];


NSStringTurn to string

string str("testStr");

NSString * aString = [NSString stringWithUTF8String:str.c_str()];