gpt4 book ai didi

ios - 使用 Swift 查询可用的 iOS 磁盘空间

转载 作者:IT王子 更新时间:2023-10-29 05:12:42 61 4
gpt4 key购买 nike

我正在尝试使用 Swift 获取可用的 iOS 设备存储空间。我找到了这个功能 here

        func deviceRemainingFreeSpaceInBytes() -> NSNumber {
let documentDirectoryPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let systemAttributes = NSFileManager.defaultManager().attributesOfFileSystemForPath(documentDirectoryPath.last as String, error: nil)
return systemAttributes[NSFileSystemFreeSize] as NSNumber
}

但是在编译时给出了这个错误:[NSObject : AnyObject]?没有名为“下标”的成员 我相信这个错误是由提到的问题引起的 here ,即 attributesOfFileSystemForPath 返回一个可选字典 ( documentation )。我从一般意义上理解这个问题,但是因为建议的解决方案涉及嵌套案例,所以我不太明白如何修复我感兴趣的函数(这对我很陌生没有帮助 swift )。有人可以建议如何使该功能起作用吗?注意:我不确定原始功能是否经过作者测试,或者它是否在 xcode 6 beta 下工作,但据我所知,它在 GM 下不起作用。

最佳答案

iOS 11 更新

下面给出的答案不再提供 iOS 11 下的准确结果。有新的容量键可以传递给 URL.resourceValues(forKeys:),提供与可用的值匹配的值设备设置。

  • static let volumeAvailableCapacityKey: URLResourceKey以字节为单位的卷可用容量键(只读)。

  • static let volumeAvailableCapacityForImportantUsageKey: URLResourceKey用于存储重要资源的卷的可用容量(以字节为单位)的键(只读)。

  • static let volumeAvailableCapacityForOpportunisticUsageKey: URLResourceKey用于存储非必要资源的卷的可用容量(以字节为单位)的键(只读)。

  • static let volumeTotalCapacityKey: URLResourceKey卷总容量的键(以字节为单位)(只读)。

来自 Apple's documentation :

Overview

Before you try to store a large amount of data locally, first verify that you have sufficient storage capacity. To get the storage capacity of a volume, you construct a URL (using an instance of URL) that references an object on the volume to be queried, and then query that volume.

Decide Which Query Type to Use

The query type to use depends on what's being stored. If you’re storing data based on a user request or resources the app requires to function properly (for example, a video the user is about to watch or resources that are needed for the next level in a game), query against volumeAvailableCapacityForImportantUsageKey. However, if you’re downloading data in a more predictive manner (for example, downloading a newly available episode of a TV series that the user has been watching recently), query against volumeAvailableCapacityForOpportunisticUsageKey.

Construct a Query

Use this example as a guide to construct your own query:

let fileURL = URL(fileURLWithPath: NSHomeDirectory() as String)
do {
let values = try fileURL.resourceValues(forKeys: [.volumeAvailableCapacityForImportantUsageKey])
if let capacity = values.volumeAvailableCapacityForImportantUsage {
print("Available capacity for important usage: \(capacity)")
} else {
print("Capacity is unavailable")
}
} catch {
print("Error retrieving capacity: \(error.localizedDescription)")
}

原始答案

可选绑定(bind)if let 在这里也适用。

我建议该函数返回一个可选的 Int64,这样它就可以返回nil 表示失败:

func deviceRemainingFreeSpaceInBytes() -> Int64? {
let documentDirectoryPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
if let systemAttributes = NSFileManager.defaultManager().attributesOfFileSystemForPath(documentDirectoryPath.last as String, error: nil) {
if let freeSize = systemAttributes[NSFileSystemFreeSize] as? NSNumber {
return freeSize.longLongValue
}
}
// something failed
return nil
}

Swift 2.1 更新:

func deviceRemainingFreeSpaceInBytes() -> Int64? {
let documentDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last!
guard
let systemAttributes = try? NSFileManager.defaultManager().attributesOfFileSystemForPath(documentDirectory),
let freeSize = systemAttributes[NSFileSystemFreeSize] as? NSNumber
else {
// something failed
return nil
}
return freeSize.longLongValue
}

Swift 3.0 更新:

func deviceRemainingFreeSpaceInBytes() -> Int64? {
let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last!
guard
let systemAttributes = try? FileManager.default.attributesOfFileSystem(forPath: documentDirectory),
let freeSize = systemAttributes[.systemFreeSize] as? NSNumber
else {
// something failed
return nil
}
return freeSize.int64Value
}

用法:

if let bytes = deviceRemainingFreeSpaceInBytes() {
print("free space: \(bytes)")
} else {
print("failed")
}

关于ios - 使用 Swift 查询可用的 iOS 磁盘空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26198073/

61 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com