- (void)getCacheSize
{
// 总大小
unsigned long long size = 0;
// 获得缓存文件夹路径
NSString *cachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
NSString *dirpath = [cachesPath stringByAppendingPathComponent:@"MP3/Abc"];
// 文件管理者
NSFileManager *mgr = [NSFileManager defaultManager];
// 获得文件夹的大小 == 获得文件夹中所有文件的总大小
// XMGLog(@"contents - %@", [mgr contentsOfDirectoryAtPath:dirpath error:nil]);
NSArray *subpaths = [mgr subpathsAtPath:dirpath];
for (NSString *subpath in subpaths) {
// 全路径
NSString *fullSubpath = [dirpath stringByAppendingPathComponent:subpath];
// 累加文件大小
size += [mgr attributesOfItemAtPath:fullSubpath error:nil].fileSize;
// NSDictionary *attrs = [mgr attributesOfItemAtPath:fullSubpath error:nil];
// size += [attrs[NSFileSize] unsignedIntegerValue];
}
XMGLog(@"%zd", size);
}
网友评论