NSMutableString到char*

Kei*_*ler 0 objective-c cstring nsstring

我可以将NSMutableString转换为char*吗?

例如

NSString* someval = @"This is a test.";
NSMutableString *temp = [NSMutableSTring stringWithString:someval];
char * temp2 = ????Not sure what to do here???;
Run Code Online (Sandbox Code Playgroud)

Jac*_*kin 5

使用-UTF8String方法:

NSString* someval = @"This is a test.";
NSMutableString *temp = [NSMutableString stringWithString:someval];
const char * temp2 = [temp UTF8String];
Run Code Online (Sandbox Code Playgroud)

  • 作为额外的(关于cstring的内存管理)来自Apple Docs:返回的C字符串会自动释放,就像返回的对象一样; 如果需要将C字符串存储在创建C字符串的自动释放上下文之外,则应复制C字符串. (3认同)