Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使用stat实用程序而不是子路径DirectoryAtPath来计算目录大小_Ios_Macos_Swift_Cocoa_Stat - Fatal编程技术网

Ios 如何使用stat实用程序而不是子路径DirectoryAtPath来计算目录大小

Ios 如何使用stat实用程序而不是子路径DirectoryAtPath来计算目录大小,ios,macos,swift,cocoa,stat,Ios,Macos,Swift,Cocoa,Stat,我一直在使用子路径directoryatpath::计算目录大小,发现它非常缓慢。经过大量的研究,我发现了stat结构,可以用来更快、更有效地实现预期的结果。如何在swift中使用stat实现此目标 下面是我当前使用子路径DirectoryAtPath::计算文件大小的算法: func calcSize(pathToDir: String) -> (Int) { let fm = NSFileManager.defaultManager() var size = 0 let

我一直在使用
子路径directoryatpath::
计算目录大小,发现它非常缓慢。经过大量的研究,我发现了
stat
结构,可以用来更快、更有效地实现预期的结果。如何在swift中使用
stat
实现此目标

下面是我当前使用
子路径DirectoryAtPath::
计算文件大小的算法:

func calcSize(pathToDir: String) -> (Int) {
let fm = NSFileManager.defaultManager()
    var size = 0
    let fileArray = (fm.subpathsOfDirectoryAtPath(pathToDir, error: nil)) as! [String]
    for file in fileArray {
        autoreleasepool({
        var fileDict: NSDictionary? = fm.attributesOfItemAtPath(pathToDir.stringByAppendingPathComponent(file), error: nil)
        size += Int(fileDict!.fileSize())
        })
    }
    return (size)
}

注:到目前为止,我发现的所有结果在objective-c和c中都有答案,但在swift中没有答案。

以下是swift中的示例用法:

var stat1 : stat  = stat()
var documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! NSString
stat(documentsPath.fileSystemRepresentation,  &stat1)
println(stat1.st_size)
编辑
我建议您使用
NSFileManager.EnumeratorUrl(u:includingPropertiesForKeys:options:errorHandler:)
。对于属性键,根据您感兴趣的内容指定
[NSURLTotalFileSizeKey]
,或者指定
[NSURLTotalFileAllocatedSizeKey]
。这将包括
stat()
不会包含的其他分叉和文件元数据

然后枚举目录中的项目,为每个项目获取
NSURL
s。您可以使用
resourceValuesForKeys()
查询单个
NSURL
的大小。(我建议使用
getResourceValue(u:forKey:)
,但在Swift中这可能有点尴尬。)


使用这种形式的枚举器的优点是,它可以在枚举目录时预取size属性。这应该比
stat()
更快。在引擎盖下,它使用了
getdirentriesattr()
,它将目录枚举与属性查询相结合。

谢谢,语法非常完美!但是,使用stat而不是attributesFiteMatPath的方法返回的大小是完全不准确的。我更新了我的答案,上面用新的代码使用统计,如果你能给它一看,这将意味着很多@Jonathan我认为您没有正确创建文件路径“filex”。您需要将“file”附加到“pathToDir”以获得正确的路径。我已经更新了我的答案。现在应该对你有用了。
  var stat1 : stat  = stat()
        var size = 0
        let fileArray = (NSFileManager.defaultManager().subpathsOfDirectoryAtPath(pathToDir as String, error: nil)) as! [String]
        for file in fileArray {

            var stat1 : stat  = stat()
            var filex =  (pathToDir as String) + "/" +  file as NSString
            stat(filex.fileSystemRepresentation,  &stat1)
            if stat(filex.fileSystemRepresentation,  &stat1) != 0 {
                println("Some error occured ")
            } else {
                println(stat1.st_size)
            }
            size += Int(Double(stat1.st_size))                
    }
}