Cocoa/Swift:循环路径中文件夹的名称
我目前正在用swift编写一个OSX应用程序,但我不知道如何循环,甚至无法获得某个路径上所有文件夹的名称。可能是Cocoa/Swift:循环路径中文件夹的名称,cocoa,swift,nsfilemanager,Cocoa,Swift,Nsfilemanager,我目前正在用swift编写一个OSX应用程序,但我不知道如何循环,甚至无法获得某个路径上所有文件夹的名称。可能是fm.enumeratorpath?我使用enumeratorurl。下面的一些代码显示了如何打印用户主目录中的目录的示例 if let dirURL = NSURL(fileURLWithPath: NSHomeDirectory()) { let keys = [NSURLIsDirectoryKey, NSURLLocalizedNameKey] let file
fm.enumeratorpath
?我使用enumeratorurl
。下面的一些代码显示了如何打印用户主目录中的目录的示例
if let dirURL = NSURL(fileURLWithPath: NSHomeDirectory()) {
let keys = [NSURLIsDirectoryKey, NSURLLocalizedNameKey]
let fileManager = NSFileManager.defaultManager()
let enumerator = fileManager.enumeratorAtURL(
dirURL,
includingPropertiesForKeys: keys,
options: (NSDirectoryEnumerationOptions.SkipsPackageDescendants |
NSDirectoryEnumerationOptions.SkipsSubdirectoryDescendants |
NSDirectoryEnumerationOptions.SkipsHiddenFiles),
errorHandler: {(url, error) -> Bool in
return true
}
)
while let element = enumerator?.nextObject() as? NSURL {
var getter: AnyObject?
element.getResourceValue(&getter, forKey: NSURLIsDirectoryKey, error: nil)
let isDirectory = getter! as Bool
element.getResourceValue(&getter, forKey: NSURLLocalizedNameKey, error: nil)
let itemName = getter! as String
if isDirectory {
println("\(itemName) is a directory in \(dirURL.absoluteString)")
//do something with element here.
}
}
}
这当然应该给出总体思路。但是,它没有在Xcode 6.1.1中编译。谢谢@MartinR,我修复了编译错误,并在操场上进行了尝试。它现在编译并运行。