Problem returning directory enumerator from NSFileManager using enumeratorAtUrl in Swift

I am trying to return an NSDirectoryEnumerator object from the enumeratorAtUrl method of the NSFileManager method. This results in a compiler error: Cannot convert the expressions type 'NSDirectoryEnumerator!' to type 'NSDirectoryEnumeratorOptions' Cannot convert the expressions type 'NSDirectoryEnumerator!' to type 'NSDirectoryEnumeratorOptions'

 let url:NSURL = NSURL(fileURLWithPath: "/") var keys:Array<AnyObject> = [NSURLNameKey, NSURLIsDirectoryKey] var manager:NSFileManager = NSFileManager.defaultManager() let enumerator:NSDirectoryEnumerator = manager.enumeratorAtURL(url,includingPropertiesForKeys: keys, options: 0, errorHandler: nil) 

This works in Obj-C, but not in Swift. Has anyone else encountered this problem?

+6
source share
3 answers

Try the following:

 let enumerator:NSDirectoryEnumerator = manager.enumeratorAtURL(url, includingPropertiesForKeys: keys, options: NSDirectoryEnumerationOptions(), errorHandler: nil) 

Or, in short, go to NSDirectoryEnumerationOptions() instead of " 0 ".

"0" is not really a member of the numbering that it is looking for.

+9
source

Swift 3.1 +

 let url = URL(fileURLWithPath: "/") let keys: [URLResourceKey] = [.nameKey, .isDirectoryKey] let manager = FileManager.default let options: FileManager.DirectoryEnumerationOptions = [.skipsHiddenFiles, .skipsPackageDescendants] let enumerator = manager.enumerator(at: url, includingPropertiesForKeys: keys, options: options, errorHandler: nil) 

Swift 2.0

No parameters:

 let enumerator = manager.enumeratorAtURL(url, includingPropertiesForKeys: keys, options: [], errorHandler: nil) 

One option:

 let enumerator = manager.enumeratorAtURL(url, includingPropertiesForKeys: keys, options: .SkipsHiddenFiles, errorHandler: nil) 

Several options:

 let options: NSDirectoryEnumerationOptions = [.SkipsHiddenFiles, .SkipsPackageDescendants] let enumerator = manager.enumeratorAtURL(url, includingPropertiesForKeys: keys, options: options, errorHandler: nil) 
+5
source

@ The answer to Kendall is ideal for most cases, but if you need to customize the behavior of an enumerator, here are a few examples.

Configure the enumerator to skip hidden files:

 let enumerator = manager.enumeratorAtURL(url, includingPropertiesForKeys: keys, options: .SkipsHiddenFiles, errorHandler: nil) 

If you need to set several parameters, you are "or" together:

 let options: NSDirectoryEnumerationOptions = .SkipsHiddenFiles | .SkipsPackageDescendants let enumerator = manager.enumeratorAtURL(url, includingPropertiesForKeys: keys, options: options, errorHandler: nil) 
+1
source

Source: https://habr.com/ru/post/970655/


All Articles