【问题标题】:How to get just the file name, not the full directory, with NSOpenPanel?如何使用 NSOpenPanel 仅获取文件名,而不是完整目录?
【发布时间】:2012-06-17 15:23:45
【问题描述】:

这是我的代码:

NSOpenPanel  *panel = [NSOpenPanel openPanel];
[panel setAllowsMultipleSelection:YES];
[panel runModal];

NSInteger count = [[panel URLs] count];

for (int i=0; i<count; i++) {
   NSLog(@"%@", [[panel URLs] objectAtIndex:i]);
}

但输出类似于file://localhost/Volumes/....

如何获取所选文件的基本名称(例如 Cat.jpg)?

【问题讨论】:

    标签: objective-c xcode macos cocoa nsopenpanel


    【解决方案1】:
    NSURL *url = ...;
    NSString *filename = [[url path] lastPathComponent];
    

    【讨论】:

    • 嗨,Jonathan Grynspan,如果 NSURL 类似于 file:///,我如何获得启动盘(根)名称
    • 如果您有新问题,应将其作为问题提出,而不是作为对现有问题的评论。
    【解决方案2】:

    如果只需要文件名,不需要任何扩展名:

    NSString *fileName = [[path lastPathComponent] stringByDeletingPathExtension];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-25
      • 2016-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-13
      • 2013-07-23
      相关资源
      最近更新 更多