在Swift中获取文件大小

我尝试了几种方法来获取文件大小,但总是得到零。

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()。

 var filePath: NSString = "your path here" var fileSize : UInt64 var attr:NSDictionary? = NSFileManager.defaultManager().attributesOfItemAtPath(filePath, error: nil) if let _attr = attr { fileSize = _attr.fileSize(); } 

在Swift 2.0中,我们使用try try catch模式,像这样:

 let filePath = "your path here" var fileSize : UInt64 = 0 do { let attr : NSDictionary? = try NSFileManager.defaultManager().attributesOfItemAtPath(filePath) if let _attr = attr { fileSize = _attr.fileSize(); } } catch { print("Error: \(error)") } 

在Swift 3.x / 4.0中:

 let filePath = "your path here" var fileSize : UInt64 do { //return [FileAttributeKey : Any] let attr = try FileManager.default.attributesOfItem(atPath: filePath) fileSize = attr[FileAttributeKey.size] as! UInt64 //if you convert to NSDictionary, you can get file size old way as well. let dict = attr as NSDictionary fileSize = dict.fileSize() } catch { print("Error: \(error)") } 

这是Biodave的答案,正确的文件pipe理器调用

 func sizeForLocalFilePath(filePath:String) -> UInt64 { do { let fileAttributes = try NSFileManager.defaultManager().attributesOfItemAtPath(filePath) if let fileSize = fileAttributes[NSFileSize] { return (fileSize as! NSNumber).unsignedLongLongValue } else { print("Failed to get a size attribute from path: \(filePath)") } } catch { print("Failed to get file attributes for local path: \(filePath) with error: \(error)") } return 0 } 

SWIFT 3来自@ Hoa的答案,加上一个让UInt64可读的string的函数。

 func sizeForLocalFilePath(filePath:String) -> UInt64 { do { let fileAttributes = try FileManager.default.attributesOfItem(atPath: filePath) if let fileSize = fileAttributes[FileAttributeKey.size] { return (fileSize as! NSNumber).uint64Value } else { print("Failed to get a size attribute from path: \(filePath)") } } catch { print("Failed to get file attributes for local path: \(filePath) with error: \(error)") } return 0 } func covertToFileString(with size: UInt64) -> String { var convertedValue: Double = Double(size) var multiplyFactor = 0 let tokens = ["bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"] while convertedValue > 1024 { convertedValue /= 1024 multiplyFactor += 1 } return String(format: "%4.2f %@", convertedValue, tokens[multiplyFactor]) } 

尝试这个。

 let MyUrl = NSURL(fileURLWithPath: "*** Custom File Path ***") let fileAttributes = try! NSFileManager.defaultManager().attributesOfItemAtPath(MyUrl.path!) let fileSizeNumber = fileAttributes[NSFileSize] as! NSNumber let fileSize = fileSizeNumber.longLongValue var sizeMB = Double(fileSize / 1024) sizeMB = Double(sizeMB / 1024) print(String(format: "%.2f", sizeMB) + " MB") 

在Swift 3.0中,请尝试以下操作:

 let fileSize = try! FileManager.default.attributesOfItem(atPath: "/bin/bash")[FileAttributeKey.size] as! Int 

甚至更好:

 let fileSize = (try! FileManager.default.attributesOfItem(atPath: "/bin/bash")[FileAttributeKey.size] as! NSNumber).uint64Value 

下面是一个紧凑的版本,用Swift 2.0 for iOS9通过Xcode7写成快乐的方法:

 func sizeForLocalFilePath(filePath:String) -> UInt64 { do { let fileAttributes = try NSFileManager().attributesOfFileSystemForPath(filePath) if let fileSize = fileAttributes[NSFileSystemSize] as? UInt64 { return fileSize } else { print("Failed to get a size attribute from path: \(filePath)") } } catch { print("Failed to get file attributes for local path: \(filePath) with error: \(error)") } return 0 } 

请享用!

Swift 4解决scheme:这个函数返回MB大小。

 func sizePerMB(url: URL?) -> Double { guard let filePath = url?.path else { return 0.0 } do { let attribute = try FileManager.default.attributesOfItem(atPath: filePath) if let size = attribute[FileAttributeKey.size] as? NSNumber { return size.doubleValue / 1000000.0 } } catch { print("Error: \(error)") } return 0.0 }