如何在 swift 2.0 中使用 AttributesOfFileSystemForpaths 获取总磁盘空间和可用磁盘空间 [英] How to get the Total Disk Space and Free Disk space using AttributesOfFileSystemForpaths in swift 2.0

查看:102
本文介绍了如何在 swift 2.0 中使用 AttributesOfFileSystemForpaths 获取总磁盘空间和可用磁盘空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在 Objective-C 中使用的方式

This is the way I use in Objective-C

-(uint64_t)getFreeDiskspace {
float totalSpace = 0;
float totalFreeSpace = 0;
NSError *error = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error: &error];

if (dictionary) {
    NSNumber *fileSystemSizeInBytes = [dictionary objectForKey: NSFileSystemSize];
    NSNumber *freeFileSystemSizeInBytes = [dictionary objectForKey:NSFileSystemFreeSize];
    totalSpace = [fileSystemSizeInBytes floatValue];
    self.totalSpace = [NSString stringWithFormat:@"%.3f GB",totalSpace/(1024*1024*1024)];
    totalFreeSpace = [freeFileSystemSizeInBytes unsignedLongLongValue];
    self.freeSpace = [NSString stringWithFormat:@"%.3f GB",totalFreeSpace/(1024*1024*1024)];

} else {
    LogError(@"Error Obtaining System Memory Info: Domain = %@, Code = %ld", [error domain], (long)[error code]);
}

return totalFreeSpace;

}

我尝试将其转换为 swift 和错误

I tried converting it to swift and errors in

if(dictionary)

attributesOfFileSystemForPaths 

已显示.有人可以帮我将其转换为 swift 2.0 吗?它会对我的项目大有裨益.提前致谢.

were shown.Can anyone help me in converting this to swift 2.0 ? It would do a world of good to my project. Thank You in Advance.

推荐答案

对于 Swift 5.1.3:

For Swift 5.1.3:

struct FileManagerUility {

    static func getFileSize(for key: FileAttributeKey) -> Int64? {
        let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)

        guard
            let lastPath = paths.last,
            let attributeDictionary = try? FileManager.default.attributesOfFileSystem(forPath: lastPath) else { return nil }

        if let size = attributeDictionary[key] as? NSNumber {
            return size.int64Value
        } else {
            return nil
        }
    }

    static func convert(_ bytes: Int64, to units: ByteCountFormatter.Units = .useGB) -> String? {
        let formatter = ByteCountFormatter()
        formatter.allowedUnits = units
        formatter.countStyle = ByteCountFormatter.CountStyle.decimal
        formatter.includesUnit = false
        return formatter.string(fromByteCount: bytes)
    }

}

像这样使用api:

 if let totalSpaceInBytes = FileManagerUility.getFileSize(for: .systemSize) {
    /// If you want to convert into GB then call like this
    let totalSpaceInGB = FileManagerUility.convert(totalSpaceInBytes)
    print("Total space [\(totalSpaceInBytes) bytes] = [\(totalSpaceInGB!) GB]")

}

if let freeSpaceInBytes = FileManagerUility.getFileSize(for: .systemFreeSize) {
    /// If you want to convert into GB then call like this
    let freeSpaceInGB = FileManagerUility.convert(freeSpaceInBytes)
    print("Free space [\(freeSpaceInBytes) bytes] = [\(freeSpaceInGB!) GB]")
}

这篇关于如何在 swift 2.0 中使用 AttributesOfFileSystemForpaths 获取总磁盘空间和可用磁盘空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆