objective-c - 将 char 转换为 NSString

标签 objective-c cocoa

我在将 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 字符串。您应该使用 %@ 而不是 %sNSString (和其他 Foundation 类型)输出到 NSLog

关于objective-c - 将 char 转换为 NSString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9503737/

相关文章:

ios - 在 Objective C 中更改 SVProgessHud 的 BG 颜色

ios - 使用 Storyboard 时实例化 ViewController

objective-c - 如何在 iCloud 中保存 NSData

cocoa - 在 Cocoa : How to open image, 中调整它的大小并用新名称保存

objective-c - Mac 分析磁盘的空闲扇区

cocoa - 有没有办法在Mac上生成标准128位GUID(UUID)?

objective-c - Xcode4中的变量值

ios - 无法使用 Objective C 在 SQLite 中执行重音/变音符号不敏感的搜索

ios - 在 Objective C 中包含多个标题的正确方法

macos - 有没有办法知道另一个应用程序是否更改了 Cocoa 中的显示分辨率?