【发布时间】:2019-08-11 00:05:52
【问题描述】:
我一直在尝试获取文档目录中所有项目的属性,例如文件类型、创建日期、文件大小。
这是我一直在使用的代码,但它只返回“NSFileTypeDirectory”
let filemgr = FileManager.default
do
{
let attribs: NSDictionary? = try filemgr.attributesOfItem(
atPath: documentDirectoryPath!) as NSDictionary
if let fileattribs = attribs
{
let type = fileattribs["NSFileType"] as! String
print("File type \(type)")
}
}
catch
{
print(error)
}
我认为它返回了 Document 文件夹的属性。
【问题讨论】:
-
那是因为您传递的是
Documents Directory的路径,而不是 documents directory 中文件的路径 。您需要在您想要属性的文档目录中传递文件的完整路径 -
不相关但
attributesOfItem(atPath返回非可选[FileAttributeKey : Any]not 可选NSDictionary并且永远不会使用error变量。
标签: ios swift nsdocumentdirectory nsattributedescription