Ios 如何使用swift 2.0中的AttributeOfficeSystemForPath获取总磁盘空间和可用磁盘空间

Ios 如何使用swift 2.0中的AttributeOfficeSystemForPath获取总磁盘空间和可用磁盘空间,ios,objective-c,swift,Ios,Objective C,Swift,这是我在Objective-C中使用的方法 -(uint64_t)getFreeDiskspace { float totalSpace = 0; float totalFreeSpace = 0; NSError *error = nil; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSDictionary *dictionary = [[N

这是我在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,并在

if(dictionary)

有人能帮我把它转换成swift 2.0吗?这对我的项目会有很大的好处。提前感谢您。

对于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 3@

更新Swift-4
我尝试将其转换为swift
然后请显示您的版本,以便我们可以帮助您修复。谢谢您的回复Eric.D。我没有完全改变它。而且在最初的几行中有错误,所以我浪费了大量的时间调试,但无法使其正常工作。。所以,你能转换它,让我知道如何做吗?我删除了它。但是等等,我会重写这个方法,然后返回给你错误的Eric。
 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]")
}
func getFreeSize() -> Int64? {
        let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
        if let dictionary = try? FileManager.default.attributesOfFileSystem(forPath: paths.last!) {
            if let freeSize = dictionary[FileAttributeKey.systemFreeSize] as? NSNumber {
                return freeSize.int64Value
            }
        }else{
            print("Error Obtaining System Memory Info:")
        }
        return nil
    }

    func getTotalSize() -> Int64?{
        let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
        if let dictionary = try? FileManager.default.attributesOfFileSystem(forPath: paths.last!) {
            if let freeSize = dictionary[FileAttributeKey.systemSize] as? NSNumber {
                return freeSize.int64Value
            }
        }else{
            print("Error Obtaining System Memory Info:")
        }
        return nil
    }
func getFreeDiskspace() -> Int64? {
        let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
        if let dictionary = try? FileManager.default.attributesOfFileSystem(forPath: paths.last!) {
            if let freeSize = dictionary[FileAttributeKey.systemFreeSize] as? NSNumber {
                return freeSize.int64Value
            }
        }else{
            print("Error Obtaining System Memory Info:")
        }
        return nil
    }

if let getFreespace = getFreeDiskspace() {
   print(getFreespace) // free disk space
}