iPhone 中 NSCachesDirectory 的大小

发布于 2024-08-20 21:07:08 字数 523 浏览 6 评论 0原文

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

谢谢。

编辑:这是我的代码。

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。我不知道出了什么问题。

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.

thanks.

Edit: here is my code.

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

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

}

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2024-08-27 21:07:08

类似以下内容应该可以帮助您入门:

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

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

我不确定 iPhone 上使用的文件系统,或者设备上的文件块大小是多少,因此虽然字节总数相同,但模拟器和设备之间的实际磁盘使用情况可能不同。

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;
}

EDIT

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

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

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.

白衬杉格子梦 2024-08-27 21:07:08

您真的是指 /Library/Cache,还是指 ~/Library/Cache(应用程序的缓存目录)。您通常无法控制前者,所以我假设您指的是后者。

使用 NSFileManager 的 -enumeratorAtPath: 遍历目录并使用 -attributesOfItemAtPath:error: 获取文件大小。我建议在后台线程上缓慢执行此操作,以避免阻塞您的应用程序。

Do you really mean /Library/Cache, or do you mean ~/Library/Cache (the application's cache directory). You generally have no control over the former, so I'll assume you mean the latter.

Use NSFileManager's -enumeratorAtPath: to walk the directory and use -attributesOfItemAtPath:error: to fetch the fileSize. I recommend doing this slowly on a background thread to avoid blocking your app.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文