iPhone 中 NSCachesDirectory 的大小
我如何获取文件夹 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
类似以下内容应该可以帮助您入门:
编辑
返回的值将以字节为单位: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:
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.
您真的是指 /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.