我想利用
NSProgress
localizedAdditionalDescription
但我无法找出组合来使其格式化文件计数。要么我没有得到任何后缀,要么我得到字节。
progress.kind: File
Processing 15 files…
5 bytes of 20 bytes
progress.kind: None
25 % completed
5 of 20
let progress = Progress(totalUnitCount: 20)
progress.completedUnitCount = 5
progress.setUserInfoObject(10, forKey: ProgressUserInfoKey.fileCompletedCountKey)
progress.setUserInfoObject(15, forKey: ProgressUserInfoKey.fileTotalCountKey)
print("progress.kind: File")
progress.kind = .file
print(progress.localizedDescription)
print(progress.localizedAdditionalDescription)
print("progress.kind: None")
progress.kind = .none
print(progress.localizedDescription)
print(progress.localizedAdditionalDescription)
获取此信息的关键是将
NSProgressByteTotalCountKey
和NSProgressByteCompletedCountKey
重置为0
progress.setUserInfoObject(0, forKey: ProgressUserInfoKey("NSProgressByteTotalCountKey"))
progress.setUserInfoObject(0, forKey: ProgressUserInfoKey("NSProgressByteCompletedCountKey"))
结果:
progress.kind: File
Processing 15 files…
10 of 15 files
progress.kind: None
25 % completed
10 of 15 files
奖励:如果您使用
NSProgressUseItemDescriptionKey
,您可以将文件更改为项目
progress.setUserInfoObject(true, forKey: ProgressUserInfoKey("NSProgressUseItemDescriptionKey"))
结果:
progress.kind: File
Processing 15 items…
10 of 15 items
progress.kind: None
25 % completed
10 of 15 items
totalUnitCount
为
kind
之外,
.file
还必须为零,才能按文件获取进度。您可以直接设置 fileTotalCount
和 fileCompletedCount
,而不是通过用户信息对象。
let progress = Progress()
progress.kind = .file
progress.fileCompletedCount = 5
progress.fileTotalCount = 20
print(progress.localizedAdditionalDescription)
输出:
Optional("5 of 20 files")