[英]How to migrate data from Alamofire closure to main thread
我正在使用Alamofire生成對隨機網頁的請求。 我想獲取標題故事的標題並將其保存在主線程中,以將其傳遞到MenuCell中。
import UIKit
import SwiftSoup
import Alamofire
class ArticleListScreen: UIViewController {
var index: Int = 0
var titles: [String] = []
var images: [UIImage] = [#imageLiteral(resourceName: "outside-page.png"),#imageLiteral(resourceName: "outside-page.png"),#imageLiteral(resourceName: "outside-page.png"),#imageLiteral(resourceName: "outside-page.png"),#imageLiteral(resourceName: "outside-page.png")]
let url: [NSURL] = [NSURL(string: "https://www.shreveporttimes.com/story/news/2019/03/12/only-louisiana-crawfish-pardoned-lent/3137805002/")!]
var articles: [Article] = []
var regular: [Regular] = []
var loaded = true
override func viewDidLoad() {
super.viewDidLoad()
parseHTML(id: "title")
articles = createArticleArray()
regular = createRegularArray()
}
func parseHTML(id: String){
var title: String = ""
Alamofire.request(url[0] as URL).responseString { response in
if let html: String = response.result.value {
NSLog(html)
do{
let doc: Document = try SwiftSoup.parse(html)
try title = doc.getElementsByTag(id).text()
NSLog("NEW SHIT: "+title)
}catch{
NSLog("None")
}
}
}
}
func createRegularArray() -> [Regular] {
var tempRegular: [Regular] = []
let regular1 = Regular(image:#imageLiteral(resourceName: "sps.jpg"), title: "NFL lawyer who claimed Super Bowl is 'rigged' is found dead")
tempRegular.append(regular1)
return tempRegular
}
func createArticleArray() -> [Article] {
var tempArticles: [Article] = []
for (image, title) in zip(images, titles) {
tempArticles.append(Article(image:image, title: title))
}
return tempArticles
}
}
extension ArticleListScreen: UITableViewDataSource, UITabBarDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return articles.count+regular.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if(indexPath.row == 0){
tableView.rowHeight = 270
tableView.separatorStyle = .singleLine
let article = regular[0]
let cell = tableView.dequeueReusableCell(withIdentifier: "RegularCell") as! RegularCell
cell.setArticle(article: article)
return cell
}
else if (indexPath.row == 1){
tableView.rowHeight = 100
//tableView.separatorStyle = .none
let article = articles[indexPath.row-1]
let cell = tableView.dequeueReusableCell(withIdentifier: "ArticleCell") as! ArticleCell
cell.setArticle(article: article)
return cell
}
else{
tableView.rowHeight = 90
//tableView.separatorStyle = .none
let article = articles[indexPath.row-1]
let cell = tableView.dequeueReusableCell(withIdentifier: "ArticleCell") as! ArticleCell
cell.setArticle(article: article)
return cell
}
}
}
請幫忙。 我似乎無法弄清楚如何在閉包之外獲取數據。 我知道閉包在不同的線程中運行,因此當我嘗試在函數中返回數據時,數據永遠都不准確。
嘗試這種方式,我也建議您閱讀有關completionHandler的內容 。
func parseHTML(id: String, completionHandler: @escaping (String) -> Void){
var title: String = ""
Alamofire.request(url[0] as URL).responseString { response in
if let html: String = response.result.value {
NSLog(html)
do{
let doc: Document = try SwiftSoup.parse(html)
try title = doc.getElementsByTag(id).text()
NSLog("NEW SHIT: "+title)
completionHandler(title)
}catch{
NSLog("None")
completionHandler("None")
}
}
}
}
//Useage
parseHTML(id: "") { result in
self.title = result
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.