First of all, you need to clarify which method you want to call:
- func resourceValues(forKeys: Set<URLResourceKey>) -> URLResourceValues
- static func resourceValues(forKeys: Set<URLResourceKey>, fromBookmarkData: Data) -> URLResourceValues?
I guess you have no intention to work with bookmarks, then use the first.
Second, to use the first method above, which is an instance method, you need to find how to get the instance of `URL` appropriate to use with `URLResourceKey.volumeAvailableCapacityForImportantUsageKey`. It's not clearly documented (see. volumeAvailableCapacityForImportantUsageKey), but some file URL included in the volume you want to inspect would work.
Third, the method is marked as `throws`, so you need to enclose the method call with do-catch and need `try`.
Fourth, the return type of the method is `URLResourceValues`, not an integer type. You need to extract the value of `volumeAvailableCapacityForImportantUsage`. Remember the type of the property is `Optional<Int64>`. You may need to use conditional-binding.
let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
do {
let values = try url.resourceValues(forKeys: [.volumeAvailableCapacityForImportantUsageKey])
if let capacity = values.volumeAvailableCapacityForImportantUsage {
print(capacity)
if itemsArray[indexPath.row].fileSize > capacity {
// alert user not enough space for file
return
}
//Continue processing which you want to do when enough capacity available
//...
} else {
print("volumeAvailableCapacityForImportantUsage not available")
return
}
} catch {
print(error)
return
}
A little bit too long than you expect? You may want to write sort of extension or something.