如何将 NSString 转换为 C 字符串? [英] How to convert NSString to C string?
本文介绍了如何将 NSString 转换为 C 字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道这个问题可能是重复的,但即使在本论坛上查看了一些 Google 教程和问题后,也没有人给我一个关于这个主题的体面答案.
I know that this question is a possible duplicate, but even after looking at some Google tutorials and questions even on this forum none of them gives me a decent answer about this subject.
我有:
NSString *str = @"text";
我想做类似的事情:
char cstring [512] = str;
(这只是显示了我想要做什么,在查看了 Apple 的 NSString Class Ref 之后我什至没有考虑使用它).
(this only shows what I want to do, after looking at Apple's NSString Class Ref I didn't even think about using it).
到目前为止我有:
char command [512] = [[NSString stringWithFormat:@"text"] cStringUsingEncoding:NSUTF8StringEncoding];
尽管如此,我还是会出错.
Still, with that I get errors.
有什么解决办法吗?
推荐答案
try const char *command = [str UTF8String];
这篇关于如何将 NSString 转换为 C 字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文