如何使用 NSProgress 的 localizedAdditionalDescription 显示“10 个文件中的 3 个”

问题描述 投票:0回答:2

我想利用

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)

enter image description here

swift macos foundation nsprogress
2个回答
0
投票

获取此信息的关键是将

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

0
投票
除了

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")
© www.soinside.com 2019 - 2024. All rights reserved.