我使用这种方法来获取磁盘上的可用空间,该磁盘是从经过一些研究后发现的代码中提取的。
float freeSpace = -1.0f;
NSError* error = nil;
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSDictionary* dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error: &error];
if (dictionary) {
NSNumber* fileSystemSizeInBytes = [dictionary objectForKey:NSFileSystemFreeSize];
freeSpace = [fileSystemSizeInBytes floatValue];
}
我想知道为什么运行此命令时会给我3660062720.000000字节的可用空间
这将产生3,408699035644531 Gb(/ 1024/1024/1024)
但是,查看我的iPhone设置->一般信息(以及iTunes),我说我的iPhone仅剩3.2 Gb。
错误在哪里?
最佳答案
似乎有时可用空间报告不正确https://discussions.apple.com/thread/2566412?threadID=2566412
编辑:我尝试下面的代码,并注意到在我的设备上,也有〜200MB差异。也许以某种方式为系统保留了存储空间?
NSDictionary *fsAttr = [[NSFileManager defaultManager] fileSystemAttributesAtPath:NSHomeDirectory()];
unsigned long long freeSpace = [[fsAttr objectForKey:NSFileSystemFreeSize] unsignedLongLongValue];
NSLog(@"%llu", freeSpace);
NSLog(@"%f", freeSpace / 1073741824.0);
关于iphone - iPhone-设备上剩余的可用空间报告错误(+-200 Mb差异),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22224915/