文件是0个字节

时间:2018-02-08 00:37:24

标签: ios swift filesystems

问题

为什么显示Developer是唯一包含任何内容的目录?有SystemLibraryApplications等目录,我知道这些目录不是0

代码

print(ViewController.getSizeOfFolders(startingAt: "/"))
    private static func getSizeOfFolders(startingAt:String) -> [Int] {
        let dirs = getFiles(inFolder: startingAt)
        var out:[Int] = []
        for dir in dirs {
            out.append(getSize(ofFolder: dir))
        }
        return out
    }

    private static func getSize(ofFolder:URL) -> Int {
        var folderSize = 0
        (try? FileManager.default.contentsOfDirectory(at: ofFolder, includingPropertiesForKeys: nil))?.lazy.forEach {
            folderSize += (try? $0.resourceValues(forKeys: [.totalFileAllocatedSizeKey]))?.totalFileAllocatedSize ?? 0
        }
        print("\(ofFolder): \(folderSize)")

        return folderSize
    }

    private static func getFiles(inFolder:String) -> [URL] {
        let fileURL = URL(fileURLWithPath:inFolder)
        do {
            let directoryContents = try FileManager.default.contentsOfDirectory(at: fileURL, includingPropertiesForKeys: nil, options: [])
            return directoryContents
        } catch {
            print(error.localizedDescription)
            return []
        }
    }

输出

file:///.HFS+%20Private%20Directory%20Data%0D/: 0
file:///.file: 0
file:///.mb/: 0
file:///Applications/: 0
file:///Developer/: 16384
file:///Library/: 0
file:///System/: 0
file:///bin/: 0
file:///cores/: 0
file:///dev/: 0
file:///etc: 0
file:///private/: 0
file:///sbin/: 0
file:///tmp: 0
file:///usr/: 0
file:///var: 0
[0, 0, 0, 0, 16384, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

0 个答案:

没有答案