这是我在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)
和
attributesOfFileSystemForPaths
显示。任何人都可以帮助我将其转换为swift 2.0?它会为我的项目做一个好的世界。提前谢谢。
答案 0 :(得分:4)
更新Swift 3 @ Md.Muzahidul Islam
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
}
答案 1 :(得分:3)
你可以这样使用
func getFreeSize() -> Int64? {
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
if let dictionary = try? NSFileManager.defaultManager().attributesOfFileSystemForPath(paths.last!) {
if let freeSize = dictionary[NSFileSystemFreeSize] as? NSNumber {
return freeSize.longLongValue
}
}else{
print("Error Obtaining System Memory Info:")
}
return nil
}
func getTotalSize() -> Int64?{
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
if let dictionary = try? NSFileManager.defaultManager().attributesOfFileSystemForPath(paths.last!) {
if let freeSize = dictionary[NSFileSystemSize] as? NSNumber {
return freeSize.longLongValue
}
}else{
print("Error Obtaining System Memory Info:")
}
return nil
}
现在将这些功能称为如下:
if let total = getTotalSize(){
print(total) // total disk space, use as you like
}
if let free = getFreeSize(){
print(free) // free disk space, use as you like
}
答案 2 :(得分:1)
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
}
答案 3 :(得分:1)
已使用Swift 5.2
(根据Vishal Vaghasiya的回答)更新了Result
。
func getFreeDiskspace() -> Result<Int64, Error> {
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
do {
let dictionary = try FileManager.default.attributesOfFileSystem(forPath: paths.last!)
let freeSize = dictionary[FileAttributeKey.systemFreeSize] as! Int64
return Result.success(freeSize)
} catch {
return Result.failure(error)
}
}