Objective-C contentsOfDirectoryAtURL需要文件名而不是完整路径

Ron*_*ann 0 filenames file-type objective-c nsfilemanager

我正在使用这段代码来获取文件夹pathTo_Folder中包含的文件.我得到的是这样的:"file://localhost/Users/ronny/DEV/0200-ObjC4/ModernTimes/DrawingFun/linen/Tech-2.jpg","file:// localhost/Users/ronny/DEV /0200-ObjC4/ModernTimes/DrawingFun/linen/Tech-3.jpg","file://localhost/Users/ronny/DEV/0200-ObjC4/ModernTimes/DrawingFun/linen/Tech-4.jpg","file ://localhost/Users/ronny/DEV/0200-ObjC4/ModernTimes/DrawingFun/linen/Terra-1.jpg","file:// localhost/Users/ronny/DEV/0200-ObjC4/ModernTimes/DrawingFun/linen /Terra-2.jpg","file://localhost/Users/ronny/DEV/0200-ObjC4/ModernTimes/DrawingFun/linen/Terra-3.jpg","file:// localhost/Users/ronny/DEV /0200-ObjC4/ModernTimes/DrawingFun/linen/Terra-4.png","file://localhost/Users/ronny/DEV/0200-ObjC4/ModernTimes/DrawingFun/linen/Terra-5.png","file ://localhost/Users/ronny/DEV/0200-ObjC4/ModernTimes/DrawingFun/linen/Terra-6.jpg"

我想知道是否有办法只获取没有像"Tech-2.jpg"这样的包含文件夹的文件名

NSString *pathTo_Folder = @"/Users/ronny/DEV/0200-ObjC4/ModernTimes/DrawingFun/linen";

NSFileManager *fileManager = [NSFileManager defaultManager];

NSArray *theFiles =  [fileManager contentsOfDirectoryAtURL:
                      [NSURL fileURLWithPath:pathTo_Folder]
                                includingPropertiesForKeys:[NSArray arrayWithObject:NSURLNameKey]
                                                   options:NSDirectoryEnumerationSkipsHiddenFiles
                                                     error:nil];
Run Code Online (Sandbox Code Playgroud)

来自瑞士的问候,罗纳德霍夫曼

Jas*_*oco 5

for( NSURL* fileURL in theFiles ) {
  NSString* filename = [fileURL lastPathComponent];
  // do things with the filename
}
Run Code Online (Sandbox Code Playgroud)

根据评论,如果你只是想要文件名:

// Note, this is not very efficient, and it's especially inefficient if
// you then go ahead and iterate the resultant array
NSArray* filenames = [theFiles valueForKeyPath:@"lastPathComponent"];
Run Code Online (Sandbox Code Playgroud)