iphone中NSCachesDirectory的大小 [英] size of NSCachesDirectory in iphone

查看:163
本文介绍了iphone中NSCachesDirectory的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取文件夹NSCachesDirectory的大小,即/ Library / Cache。我想知道这个文件夹的大小,这样我最终可以清除它。

how do i get size of folder NSCachesDirectory i.e /Library/Cache. i want to know size of this folder so that i can eventually clear this.

谢谢。

编辑:这是我的代码。

NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:folderPath error:&error];
if (attributes != nil) {

    if (fileSize = [attributes objectForKey:NSFileSize]) {
        NSLog(@"size of :%@ = %qi\n",folderPath, [fileSize unsignedLongLongValue]);
    }

}

当我运行它时它给了我文件大小768(不知道字节或KB)我检查查找它显示文件夹大小168KB。我不知道什么是错的。

when i run this it gives my file size 768(dont know bytes or KB) and i check in finder it shows me folder size 168KB. i dont know whats wrong.

推荐答案

以下内容应该有助于您入门:

Something like the following should help get you started:

- (unsigned long long int) cacheFolderSize {
    NSFileManager *_manager = [NSFileManager defaultManager];
    NSArray *_cachePaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *_cacheDirectory = [_cachePaths objectAtIndex:0]; 
    NSArray *_cacheFileList;
    NSEnumerator *_cacheEnumerator;
    NSString *_cacheFilePath;
    unsigned long long int _cacheFolderSize = 0;

    _cacheFileList = [_manager subpathsAtPath:_cacheDirectory];
    _cacheEnumerator = [_cacheFileList objectEnumerator];
    while (_cacheFilePath = [_cacheEnumerator nextObject]) {
        NSDictionary *_cacheFileAttributes = [_manager fileAttributesAtPath:[_cacheDirectory stringByAppendingPathComponent:_cacheFilePath] traverseLink:YES];
        _cacheFolderSize += [_cacheFileAttributes fileSize];
    }

    return _cacheFolderSize;
}

编辑

返回的值将以字节为单位:cf。 http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/Reference/Reference.html#//apple_ref/doc/c_ref/NSFileSize

The value returned will be in bytes: cf. http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/Reference/Reference.html#//apple_ref/doc/c_ref/NSFileSize

假设你在模拟器中运行它,Finder可能会报告这些字节使用文件块。这些块必然大于文件数据本身。阅读HFS +系统以了解块: http://en.wikipedia.org/wiki/HFS_Plus

Assuming you are running this in the Simulator, Finder is probably reporting usage of file blocks for those bytes. Those blocks will necessarily be larger than the file data itself. Read up on the HFS+ system to learn about blocks: http://en.wikipedia.org/wiki/HFS_Plus

我不确定iPhone上使用的是什么文件系统,或者设备上的文件块大小是什么,所以虽然字节总数将是同样,模拟器和设备之间的实际磁盘使用量可能不同。

I'm not sure what file system is used on the iPhone, or what the file block size will be on the device, so while the byte total will be the same, the actual disk usage may be different between Simulator and device.

这篇关于iphone中NSCachesDirectory的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆