swift中的Dispatchqueue和SVProgressHUD

swift中的Dispatchqueue和SVProgressHUD,swift,svprogresshud,dispatch-queue,Swift,Svprogresshud,Dispatch Queue,我一直在设计一个分析文本行的应用程序,我想用SVProgressHUD来显示它的进度 这是我的代码: let total = text.count for line in text{ count = count + 1 DispatchQueue.global(pos: .background).async{ //Analyzing line DispatchQueue.main.async{ SVProgressHUD.s

我一直在设计一个分析文本行的应用程序,我想用SVProgressHUD来显示它的进度

这是我的代码:

let total = text.count
for line in text{
    count = count + 1
    DispatchQueue.global(pos: .background).async{
        //Analyzing line
        DispatchQueue.main.async{
            SVProgressHUD.showProgress(count/total)
        }
    }
}
计数
达到
总计
时,分析工作正常,HUD正确显示,进程卡住,SVPROGRESS HUD在max状态停止,程序停止。这个计划有什么问题

我是否使用Dispatchqueue错误? 我是否需要调用其他程序来释放后台进程或其他程序

我尝试过交换
//分析行
SVProgressHUD.show…
,但仍然不起作用。我最初在没有dispatchqueue的循环中使用SVProgress,但是只有在分析(完整循环)完成后,progress hud才会移动,这是一个问题

任何帮助都将不胜感激


谢谢。

使用下面的字符串扩展名分析您的字符串。它有一个完成块,它将返回进度和完成状态

extension String {
func analyseString(completion: @escaping (Bool, Float) -> ()) {
    let totalCountOfString = self.count
    for (index, _) in self.enumerated() {
        if index == totalCountOfString - 1 {
            completion(true, Float(index)/Float(totalCountOfString))
        } else {
            completion(false, Float(index)/Float(totalCountOfString))
        }
    }
  }
}
您可以调用上面的方法来显示您的进度,如下所示(可能通过单击按钮)<代码>自我。yourString
是您需要分析的输入字符串

@IBAction func clicked(_ sender: UIButton) {
    DispatchQueue.main.async {
        self.yourString.analyseString { (isCompleted, progress) in
            if isCompleted {
                SVProgressHUD.dismiss()
                print("Ending")
            } else {
                SVProgressHUD.showProgress(progress, status: "Analysing (\(progress)%)")
            }
        }
    }
}

尝试使用此代码。它不使用循环,而是实现对函数的递归调用,以处理字符串数据

func processLines() {
    SVProgressHUD.showProgress(0)

    // sample data
    var text = Array("abcdefghijklmnop")
    var lines : [Line] = []
    let count : [Int] = Array(0..<text.count)
    count.forEach({ pos in lines.append(Line(char: text[pos])) })
    var currentIndex : Int = 0

    func processLine(at index: Int) {

        DispatchQueue.global(qos: .background).async{
            //Analyzing line
            let line = lines[index]
            print("Processing Line CHAR: \(line.char)")

            DispatchQueue.main.async{
                DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                    guard currentIndex < lines.count-1 else {
                        SVProgressHUD.showProgress(1)
                        return
                    }
                    currentIndex += 1
                    startLineProces(at: currentIndex)
                }
            }
        }

    }

    func startLineProces(at index: Int) {
        processLine(at: index)
        SVProgressHUD.showProgress(Float(index) / Float(lines.count))
    }


    DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
        startLineProces(at: currentIndex)
    }
}

struct Line {
   var char: Character
   init(char: Character) {
     self.char = char
   }
}
func processLines(){
SVProgressHUD.showProgress(0)
//样本数据
var text=Array(“abcdefghijklmnop”)
变量行:[Line]=[]

let count:[Int]=Array(0..当它达到100%时会发生什么?@RakeshaShastri应该退出循环并删除ProgressHUD。或者在代码中尝试以下操作:SVProgressHUD.showProgress(Float(count)/Float(total))我尝试了你的代码,计算和HUD显示代码一起工作(我尝试了打印语句),但在计算完成之前,HUD进度不会显示。SVProgressHUD.showProgress行会运行,但实际上不会显示。@Mininny尝试输入大字符串(可能有1000个字符长)要看到明显的进展。我正在使用一个有大约10K行字符串的文本,这相当长。我认为背景处理有问题,但我不确定为什么HUD不会出现。