我正在创建一个文件夹,用我的iPhone App缓存Documents中的图像.我希望能够将此文件夹的大小保持在1MB,因此我需要检查文件夹的大小(以字节为单位).
最好的方法是什么?
我创建了一个Temp目录来存储一些文件:
//MARK: -create save delete from directory
func createTempDirectoryToStoreFile(){
var error: NSError?
let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
let documentsDirectory: AnyObject = paths[0]
tempPath = documentsDirectory.stringByAppendingPathComponent("Temp")
if (!NSFileManager.defaultManager().fileExistsAtPath(tempPath!)) {
NSFileManager.defaultManager() .createDirectoryAtPath(tempPath!, withIntermediateDirectories: false, attributes: nil, error: &error)
}
}
Run Code Online (Sandbox Code Playgroud)
没关系,现在我要删除目录中的所有文件......我试过如下:
func clearAllFilesFromTempDirectory(){
var error: NSErrorPointer = nil
let dirPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! String
var tempDirPath = dirPath.stringByAppendingPathComponent("Temp")
var directoryContents: NSArray = fileManager.contentsOfDirectoryAtPath(tempDirPath, error: error)!
if error == nil {
for path in directoryContents {
let fullPath = dirPath.stringByAppendingPathComponent(path …Run Code Online (Sandbox Code Playgroud)