【发布时间】:2015-03-31 19:59:07
【问题描述】:
我尝试了几种方法来获取文件大小,但总是为零。
let path = NSBundle.mainBundle().pathForResource("movie", ofType: "mov")
let attr = NSFileManager.defaultManager().attributesOfFileSystemForPath(path!, error: nil)
if let attr = attr {
let size: AnyObject? = attr[NSFileSize]
println("File size = \(size)")
}
我进入日志:File size = nil
【问题讨论】:
-
您确定该文件位于顶层的应用程序包中吗?
-
或者尝试使用 attributesOfItemAtPath 而不是 attributesOfFileSystemForPath。您可以在 attr 上调用 .fileSize() 来获取文件大小。
-
@tyt_g207 我相信这是正确的解决方案,但您可能应该将其写为答案而不是评论,以便将来寻求答案的人更容易找到。