Swift 如何将progressView的进度作为一个元素存储到arraylist中?
我想将progressView的进度作为一个元素存储到arraylist中 我像这样清除了阵列Swift 如何将progressView的进度作为一个元素存储到arraylist中?,swift,xcode,arraylist,uiprogressview,Swift,Xcode,Arraylist,Uiprogressview,我想将progressView的进度作为一个元素存储到arraylist中 我像这样清除了阵列 var prog = [Float]() func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
var prog = [Float]()
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
if totalBytesExpectedToWrite > 0 {
progressVi = Float(totalBytesWritten) / Float(totalBytesExpectedToWrite)
print(progressVi)
}
}
[0.0071399305, 0.004518387, 0.002777841, 0.0012658517, 0.00086148235, 0.00063292583, 0.000580182, 0.0005274382, 0.0001557393]
我正在取得这样的进展
var prog = [Float]()
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
if totalBytesExpectedToWrite > 0 {
progressVi = Float(totalBytesWritten) / Float(totalBytesExpectedToWrite)
print(progressVi)
}
}
[0.0071399305, 0.004518387, 0.002777841, 0.0012658517, 0.00086148235, 0.00063292583, 0.000580182, 0.0005274382, 0.0001557393]
我试着用这个来储存它
prog.insert(progressVi, at: 0)
但我是这样理解的
var prog = [Float]()
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
if totalBytesExpectedToWrite > 0 {
progressVi = Float(totalBytesWritten) / Float(totalBytesExpectedToWrite)
print(progressVi)
}
}
[0.0071399305, 0.004518387, 0.002777841, 0.0012658517, 0.00086148235, 0.00063292583, 0.000580182, 0.0005274382, 0.0001557393]
而且它一直在添加
那么如何将其存储为一个元素
请帮助我,因为我在互联网上找不到解决方案
谢谢存储单个元素
要将其存储为数组的单个元素,只需执行以下操作:
var prog = [Float]()
prog = [progresVi]
如果在0
处插入项,则每次插入元素时,数组都会增大
存储多个元素
为了存储多个进程,有必要创建一个模型来存储队列
struct Element {
let id: UUID
var progress: Float
}
var stack = [Element]()
必须为正在注册进度的每个项目存储ID。如果您有ID,则可以更新项目进度:
let idForItem = UUID()
func getProgress() -> Float { ... }
let newProgress = getProgress()
// Update the stack of elements
if let elementIndice = stack.firstIndex(where: {$0.id == idForItem}) {
stack[elementIndice].progress = newProgress
}
你走错方向了。您需要的是获取dataTask对象并使用它设置属性 设置此属性后,进度视图将使用从对象接收的信息自动更新其进度值。(进度更新已设置动画。)如果要手动更新进度,请将属性设置为nil。此属性的默认值为零。 有关配置进度对象以管理进度信息的详细信息,请参阅
只需通过订阅语法
prog[0]=progressVi
@draeamswind.访问要分配值的元素。。致命错误:索引超出范围:很好,工作正常。但当我下载另一个文件时。第二个进展是在第一个基础上添加。。。但不是作为第二个元素如果你想存储多个项目,你需要建模,让我把它编码成一个版本。。。