对循环内的异步块感到困惑

时间:2021-05-04 09:04:52

标签: swift swift3 swift4 swift5

我只想在执行完闭包块内的所有循环后才打印数据,因为我不知道如何准确使用调度组。我尝试了多种方法。

我在这里做错了什么?

private func recognizeText(images: [UIImage]) {
    
    let myDispatchGroup = DispatchGroup()
    
    self.extractedTextfromImages(images: images) { blocks in
        myDispatchGroup.enter()
        
        for block in blocks {
            
            for line in block.lines {
                
                
                //save emirate id
                if isValidEmiratesID(emiratesID: line.text) == true {
                    
                    let id  = line.text
                    self.dt.id = id
                }
                
                // save name
                if line.text.lowercased().range(of: "name") != nil {
                    
                    
                    if let range = line.text.range(of: ":") {
                        let nm = line.text[range.upperBound...]
                        let name = String(nm.trimmingCharacters(in: .whitespaces))
                        self.dt.name = name
                    }
                }
                
            }
            
        }
        myDispatchGroup.leave()
    }
    
    myDispatchGroup.notify(queue: .main) {
        print("data is \(self.dt)")
    }
    
    
    
}

0 个答案:

没有答案