Instance Method インスタンスメソッド

enumerator(at:includingPropertiesForKeys:options:errorHandler:)

Returns a directory enumerator object that can be used to perform a deep enumeration of the directory at the specified URL. 指定されたURLでのディレクトリの深い列挙を実行するために使用できるディレクトリ列挙子オブジェクトを返します。

Declaration 宣言

@nonobjc func enumerator(at url: URL, includingPropertiesForKeys keys: [URLResourceKey]?, options mask: FileManager.DirectoryEnumerationOptions = [], errorHandler handler: ((URL, Error) -> Bool)? = nil) -> FileManager.DirectoryEnumerator?

Parameters パラメータ

url

The location of the directory for which you want an enumeration. This URL must not be a symbolic link that points to the desired directory. You can use the resolvingSymlinksInPath method to resolve any symlinks in the URL. それに対してあなたが列挙を望むディレクトリの場所。このURLは、望むディレクトリを教えるシンボリックリンクであることはできません。あなたは、resolvingSymlinksInPathメソッドを使って、URLの中のあらゆるシンボリックリンクを解決できます。

keys

An array of keys that identify the properties that you want pre-fetched for each item in the enumeration. The values for these keys are cached in the corresponding NSURL objects. You may specify nil for this parameter. For a list of keys you can specify, see URLResourceKey. 列挙の中の各項目に対して事前読み込みされることをあなたが望むプロパティを識別するキーからなる配列。これらキーに対する値は、対応するNSURLオブジェクトの中にキャッシュされます。あなたは、nilをこのパラメータに指定するかもしれません。あなたが指定できるキーの一覧として、URLResourceKeyを見てください。

mask

Options for the enumeration. For a list of valid options, see FileManager.DirectoryEnumerationOptions. 列挙に対するオプション。有効なオプションの一覧として、FileManager.DirectoryEnumerationOptionsを見てください。

handler

An optional error handler block for the file manager to call when an error occurs. The handler block should return true if you want the enumeration to continue or false if you want the enumeration to stop. The block takes the following parameters: ファイルマネージャに対する任意のエラーハンドラブロック、エラーが発生する場合に呼び出すことになります。ハンドラブロックは、あなたが列挙を継続したいならばtrue、あなたが列挙を停止したいならばfalseを返すべきです。ブロックは、次のパラメータをとります:

url

A URL that identifies the item for which the error occurred. それに対してエラーが発生した項目を識別するURL

error

An NSError object that contains information about the error. エラーについての情報を含んでいるNSErrorオブジェクト。

If you specify nil for this parameter, the enumerator object continues to enumerate items as if you had specified a block that returned true. あなたがnilをこのパラメータに対して指定するならば、列挙子オブジェクトは項目の列挙を継続します、まるであなたがtrueを返したブロックを指定していたかのように。

Return Value 戻り値

An directory enumerator object that enumerates the contents of the directory at url. If url is a filename, the method returns an enumerator object that enumerates no files—the first call to nextObject() returns nil. あるディレクトリ列挙子オブジェクト、それはurlでのディレクトリの内容を列挙します。urlがファイル名ならば、このメソッドはまったくファイルを列挙しない列挙子オブジェクトを返します — nextObject()への最初の呼び出しはnilを返します。

Discussion 議論

Because the enumeration is deep—that is, it lists the contents of all subdirectories—this enumerator object is useful for performing actions that involve large file-system subtrees. If the method is passed a directory on which another file system is mounted (a mount point), it traverses the mount point. This method does not resolve symbolic links or mount points encountered in the enumeration process, nor does it recurse through them if they point to a directory. 列挙は深くまで達することから — すなわち、それは下位ディレクトリの内容を一覧にします — この列挙子オブジェクトは多数のファイルシステム下位ツリーを伴うさまざまな動作を実行するのに有用です。メソッドがそこにおいて別のファイルシステムがマウントされるディレクトリ(マウントポイント)を渡されるならば、それはそのマウントポイントを辿っていきます。このメソッドは、列挙する過程において出くわすシンボリックリンクまたはマウントポイントそれらを解決しませんし、それらがディレクトリを指すならばそれらを通して再帰もしません。

For example, if you pass a URL that points to /Volumes/MyMountedFileSystem, the returned enumerator will include the entire directory structure for the file system mounted at that location. If, on the other hand, you pass /Volumes, the returned enumerator will include /Volumes/MyMountedFileSystem as one of its results, but will not traverse into the file system mounted there. 例えば、あなたが/Volumes/MyMountedFileSystemを示すURLを渡すならば、返される列挙子は、その場所でマウントされるファイルシステムのディレクトリ構造体全体を含むでしょう。他方、あなたが/Volumesを渡すならば、返される列挙子は/Volumes/MyMountedFileSystemをそれの結果の1つとして含むでしょう、しかしそこでマウントされるファイルシステムの中へと辿っていきません。

The FileManager.DirectoryEnumerator class has methods for skipping descendants of the existing path and for returning the number of levels deep the current object is in the directory hierarchy being enumerated (where the directory passed to enumerator(at:includingPropertiesForKeys:options:errorHandler:) is considered to be level 0). FileManager.DirectoryEnumeratorクラスは、既存のパスの子孫をとばして省くためにそして現在のオブジェクトがある列挙されているディレクトリ階層中での深さの水準数を返すためにメソッドを持ちます(そこでenumerator(at:includingPropertiesForKeys:options:errorHandler:)に渡されるディレクトリは水準0とみなされます。

This code fragment enumerates a URL and its subdirectories, collecting the URLs of files (skips directories). It also demonstrates how to ignore the contents of specified directories, in this case directories named “_extras”. このコード断片はあるURLとそれの下位ディレクトリを列挙して、ファイルのURLを収集します(ディレクトリをとばします)。それはまた指定されたディレクトリ、この場合 “_extras” と名前をつけられるディレクトリの内容を無視する方法を実演します。


let directoryURL = Bundle.main.bundleURL
let localFileManager = FileManager()
 
let resourceKeys = Set<URLResourceKey>([.nameKey, .isDirectoryKey])
let directoryEnumerator = localFileManager.enumerator(at: directoryURL, includingPropertiesForKeys: Array(resourceKeys), options: .skipsHiddenFiles)!
 
var fileURLs: [URL] = []
for case let fileURL as URL in directoryEnumerator {
    guard let resourceValues = try? fileURL.resourceValues(forKeys: resourceKeys),
        let isDirectory = resourceValues.isDirectory,
        let name = resourceValues.name
        else {
            continue
    }
    
    if isDirectory {
        if name == "_extras" {
            directoryEnumerator.skipDescendants()
        }
    } else {
        fileURLs.append(fileURL)
    }
}
 
print(fileURLs)

See Also 参照

Discovering Directory Contents ディレクトリ内容を発見する