0

このコードを使用して、フォルダー pathTo_Folder に含まれるファイルを取得しています。私が得たものは次のようなものです: /0200-ObjC4/ModernTimes/DrawingFun/linen/Tech-3.jpg", "ファイル://localhost/Users/ronny/DEV/0200-ObjC4/ModernTimes/DrawingFun/linen/Tech-4.jpg", "ファイル://localhost/Users/ronny/DEV/0200-ObjC4/ModernTimes/DrawingFun/linen/Terra-1.jpg", "ファイル://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","ファイル:

「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];

スイスからのご挨拶、ロナルド・ホフマン

4

2 に答える 2

5
for( NSURL* fileURL in theFiles ) {
  NSString* filename = [fileURL lastPathComponent];
  // do things with the filename
}

コメントに従って、ファイル名だけが必要な場合:

// 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"];
于 2012-10-16T02:48:12.863 に答える
1

NSURL には、lastPathComponentまさに必要なことを行うメソッドがあります。

于 2012-10-16T02:48:04.610 に答える