我一直在尝试获取文档目录中所有项目的文件类型,创建日期,文件大小等属性。
这是我一直在使用的代码,但它只返回“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文件夹的属性。
试试这个
let fileManager = FileManager.default
let documentdirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first
let filePath = documentdirectory?.appendingPathComponent("your file name").path
do {
let fileAttribute = try fileManager.attributesOfItem(atPath: filePath!)
let fileSize = fileAttribute[FileAttributeKey.size] as! Int64
let fileType = fileAttribute[FileAttributeKey.type] as! String
let filecreationDate = fileAttribute[FileAttributeKey.creationDate] as! Date
} catch let error {
print(error.localizedDescription)