Help us understand the problem. What is going on with this article?

配列の個数がなぜか0になる

AlamofireとKannaを使ってスクレイピングしてみました

gameのキャラクターの情報をサイトから取ってくるアプリを作ろうと思っています。
最初の画面(viewController)でURLを5個まで貼ってもらい、userDefaultsで保存して画面遷移しデータを取ってきてviewで表形式でそれぞれの値を比較しようと思いました。
スクリーンショット 2020-02-15 21.09.53.png

ViewController.swift
    var urlArray:[String] = []
    @IBOutlet weak var textField1: UITextField!
    @IBOutlet weak var textField2: UITextField!
    @IBOutlet weak var textField3: UITextField!
    @IBOutlet weak var textFiled4: UITextField!
    @IBOutlet weak var textField5: UITextField!



    @IBAction func buttonPushed(_ sender: UIButton) {
        if self.textField1.text != ""{
             self.urlArray.append(self.textField1.text!)
        }
        if self.textField2.text != ""{
             self.urlArray.append(self.textField2.text!)
        }
        if self.textField3.text != ""{
             self.urlArray.append(self.textField3.text!)
        }
        if self.textFiled4.text != ""{
             self.urlArray.append(self.textFiled4.text!)
        }
        if self.textField5.text != ""{
             self.urlArray.append(self.textField5.text!)
        }
        let userDefaults = UserDefaults.standard
        userDefaults.set(self.urlArray, forKey: "urlArray")
        userDefaults.synchronize()
        performSegue(withIdentifier: "goPlayers", sender: nil)
    }


}

PlayersViewContrller.swift
var players:[Player] = []

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        let userDefaults = UserDefaults.standard
        let urlArray:[String] = userDefaults.array(forKey: "urlArray") as! [String]

        for i in 0..<urlArray.count{
            DispatchQueue.global().sync {
                print(urlArray[i])
                self.scrapWebsite(urlArray[i])
                print("players.count3:\(self.players.count)")
            }
            print("players.count1:\(self.players.count)")
        }
        print(self.players.count)
        //viewを追加する関数
        self.SetView(players.count)
    }

///////////////////////////////////////////////////    
    struct Player {
        var playerName:String?
        var MaxLevel:String?
        var abilityList:[(name:String,score:String)] = [/*省略しますがタプル配列で26個分、予め項目名(name)と数値(score)を入れて置いてます*/]
    }
//////////////////////////////////////////////////////

func scrapWebsite(_ url:String){
        //GETリクエスト 指定URLのコードを取得
        Alamofire.request(url).responseString { response in
            //Boolで確認
            print("true or fauls:\(response.result.isSuccess)")

            if let html = response.result.value {
                //plaers配列に追加
                self.players.append(self.parseHTML(html: html))
                print("players.count2:\(self.players.count)")
            }
        }
    }

    func parseHTML(html: String)-> (Player){
        //戻り値を宣言
        var returnPlayer = Player()

        if let doc = try? HTML(html: html, encoding: .utf8) {
            //タイトルをプリント
            print("doc.titile:\(doc.title!)")

            for link in doc.xpath("/html/body/div[2]/div[4]/script[1]/text()"){
            //省略しますがreturnPlayerに値を入れる処理をします
            }
        }
        print("returnPlayer:\(returnPlayer)")
        return returnPlayer
    }

躓いているところはAlamofireとKannaとは全く関係ないところです

ViewControllerでtextField1のみにURLを入れれ実行します。
するとscrapWeb()でplayers配列にperseHTML()の戻り値(returnPlayer)を追加しているのにprint("players.count2:(self.players.count)")ではplayers.countが1となりますが、print("players.count1:(self.players.count)"),print("players.count3:(self.players.count)")では0となってしまします。
これは何が原因なのでしょうか?

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした