简体   繁体   中英

How to get just the file name, not the full directory, with NSOpenPanel?

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/....

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM