size of NSCachesDirectory in iphone

后端 未结 2 734
梦谈多话
梦谈多话 2021-01-30 19:12

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: h

2条回答
  •  南方客
    南方客 (楼主)
    2021-01-30 19:22

    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.

提交回复
热议问题