我在将 Cocoa 应用程序的参数转换为 NSString 格式时遇到问题。我像这样开始我的应用程序:
open my.app --args a1 a2
我尝试像这样访问参数:
const char *h_path_char = [[[[NSProcessInfo processInfo] arguments] objectAtIndex:1] fileSystemRepresentation];
const char *s_path_char = [[[[NSProcessInfo processInfo] arguments] objectAtIndex:2] fileSystemRepresentation];
NSString *h_path = [NSString stringWithUTF8String:h_path_char];
NSString *s_path = [NSString stringWithUTF8String:s_path_char];
NSLog(@"%s", h_path);
NSLog(@"%s", s_path);
但是,Xcode 会提示 NSLog
并显示以下警告:
Conversion specifies type "char" but the argument has type "NSString".
我该如何克服这个问题?
最佳答案
%s
适用于 C 字符串。您应该使用 %@
而不是 %s
将 NSString
(和其他 Foundation 类型)输出到 NSLog
。
关于objective-c - 将 char 转换为 NSString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9503737/