如何使用NSOpenPanel只获得文件名而不是完整目录?

时间:2023-01-17 00:00:03

This is my code:

这是我的代码:

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]);
}

But the output is something like file://localhost/Volumes/....

但是输出文件:/ / localhost /卷/ ....

How can I just get the basename of the selected file (e.g Cat.jpg)?

如何获得所选文件的basename (e)。g Cat.jpg)?

2 个解决方案

#1


3  

NSURL *url = ...;
NSString *filename = [[url path] lastPathComponent];

#2


3  

In the case only filename, without any extension, is needed:

在这种情况下,只需要文件名,不需要任何扩展名:

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

#1


3  

NSURL *url = ...;
NSString *filename = [[url path] lastPathComponent];

#2


3  

In the case only filename, without any extension, is needed:

在这种情况下,只需要文件名,不需要任何扩展名:

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