let
manager =
NSFileManager
.defaultManager()
urlForDocument = manager.
URLsForDirectory
(
NSSearchPathDirectory
.
DocumentDirectory
,inDomains:
NSSearchPathDomainMask
UserDomainMask
)
url = urlForDocument[0]
as
NSURL
//(1)对指定路径执行浅搜索,返回指定目录路径下的文件、子目录及符号链接的列表
contentsOfPath = try? manager.contentsOfDirectoryAtPath(url.path!)
//contentsOfPath:Optional([fold1,test1.txt])
print
(
"contentsOfPath: (contentsOfPath)"
)
//(2)类似上面的,对指定路径执行浅搜索,返回指定目录路径下的文件、子目录及符号链接的列表
contentsOfURL = try? manager.contentsOfDirectoryAtURL(url,includingPropertiesForKeys:
nil
NSDirectoryEnumerationOptions
SkipsHiddenFiles
);
//contentsOfURL:Optional([file://Users/.../Application/.../Documents/fold1/,
//file://Users/.../Application/.../Documents/test1.txt])
"contentsOfURL: (contentsOfURL)"
)
//(3)深度遍历,会递归遍历子文件夹(但不会递归符号链接)
enumeratorAtPath = manager.enumeratorAtPath(url.path!)
//enumeratorAtPath:Optional([fold1,fold1/test2.txt,test1.txt])
"enumeratorAtPath: (enumeratorAtPath?.allObjects)"
)
//(4)类似上面的,深度遍历,会递归遍历子文件夹(但不会递归符号链接)
enumeratorAtURL = manager.enumeratorAtURL(url,errorHandler:
)
//file://Users/.../Application/.../Documents/fold1/test2.txt,
file://Users/.../Application/.../Documents/test1.txt])
"enumeratorAtURL: (enumeratorAtURL?.allObjects)"
)
//(5)深度遍历,会递归遍历子文件夹(包括符号链接,所以要求性能的话用enumeratorAtPath)
subPaths = manager.subpathsAtPath(url.path!)
//subPaths:Optional([fold1,test1.txt])
"subPaths: (subPaths)"
)