管理从url下载的数据-Swift

时间:2019-04-11 17:09:32

标签: swift xcode parsing url swift4

我有两个URL可以从中下载数据,这些URL给了我这种json:

1)http://192.168.178.77/MyWebService/api/getteams.php

R->

{"teams":
[{"id":1,"name":"Avengers","member":7},{"id":2,"name":"Fantastics4","member":4},{"id":3,"name":"test","member":6},{"id":4,"name":"teamtest","member":33},{"id":5,"name":"inserisco","member":33},{"id":6,"name":"sarto","member":33},
{"id":7,"name":"name","member":22},
{"id":8,"name":"test","member":999}]
}

2)http://127.0.0.1/MyWebService/api/fetch_image.php?id_team=1 //其中id_team param是从第一个URL下载的ID

R-> {"image_team":[{"id":4,"img_path":"http:\/\/localhost\/MyWebService\/images\/imgTest.png","id_team":1}]}

现在,要下载此数据,我迅速创建了一些类和函数来包含和下载数据。

相应文件中的2个类。swift是:

1)在TeamJson.swift中:

import Foundation

class ClassJsonTeam: Codable {

    private var teams: [JsonTeam]

    init(teams: [JsonTeam]) {
        self.teams = teams
    }

    func getTeams()-> [JsonTeam]{
        return(self.teams);
    }

    func setTeams(teams:[JsonTeam]){
        self.teams = teams;
    }

}

class JsonTeam: Codable {
    private let id: Int
    private var name: String
    private var member: Int

    init(id: Int, name: String, member: Int) {
        self.id = id
        self.name = name
        self.member = member
    }

    func getId()->Int{
        return(self.id);
    }

    func setId(id:Int){
        self.member = id;
    }

    func getName()->String{
        return(self.name);
    }

    func setName(name:String){
        self.name = name;
    }

    func getMembers()->Int{
        return(self.member);
    }

    func setMembers(members:Int){
        self.member = members;
    }
}

2)在ImageJsonTeam.swift中

import Foundation

class ClassJsonTeamImage : Codable {
    private var teams : [JsonTeamImg]

    init(teams: [JsonTeamImg]) {
        self.teams = teams
    }

    func getTeamsImg()-> [JsonTeamImg]{
        return(self.teams);
    }

    func setTeamsImg(teams:[JsonTeamImg]){
        self.teams = teams;
    }
}

class JsonTeamImg : Codable{
    private var id : Int
    private var imagePath: URL
    private var teamId : Int

    init(id : Int, imagePath : URL , teamId : Int) {
        self.id = id
        self.imagePath = imagePath
        self.teamId = teamId
    }

    func getId() -> Int{
        return(self.id)
    }

    func setId(id : Int){
        self.id = id
    }

    func  getImagePath() -> URL {
        return(self.imagePath)
    }

    func setImagePath(imagePath : URL){
        self.imagePath = imagePath
    }

    func getTeamId()-> Int{
        return(self.teamId)
    }

    func setTeamId(teamId : Int){
        self.teamId = teamId
    }
}

此外,由于我需要合并两个json中的信息,因此我创建了这种类型的第三类:

3)CompleteTeamJson.swift

import Foundation

public class ClassJsonCompleteTeam{
    private var team : [JsonCompleteTeam]

    init(team : [JsonCompleteTeam]){
        self.team = team
    }
}

public class JsonCompleteTeam{
    private var id : Int
    private var name : String
    private var member : Int
    private var imgUrl : URL

    init(id: Int, name: String, member: Int, imgUrl: URL) {
        self.id = id
        self.name = name
        self.member = member
        self.imgUrl = imgUrl
    }

    func getId()->Int{
        return(self.id);
    }

    func setId(id:Int){
        self.member = id;
    }

    func getName()->String{
        return(self.name);
    }

    func setName(name:String){
        self.name = name;
    }

    func getMembers()->Int{
        return(self.member);
    }

    func setMembers(members:Int){
        self.member = members;
    }

    func  getImageUrl() -> URL {
        return(self.imgUrl)
    }

    func setImagePath(imgUrl : URL){
        self.imgUrl = imgUrl
    }

}

现在,我的viewController中的情况是这样:

import UIKit

//var teamCollection  : ClassJsonTeam!
//
class ViewController: UIViewController {

    @IBAction func InsertNewTeamButton(_ sender: Any) {
        performSegue(withIdentifier: "NewTeamSegue", sender: self)
    }

    var teams: [JsonTeam]?
    var teamsImg : [JsonTeamImg]?
    var teamsComplete : [JsonCompleteTeam]?

    override func viewDidLoad() {
        super.viewDidLoad()

        downloadTeams(completion: { (teams) in

            self.teams = teams

            for index in (0...(self.teams!.count - 1)){

                self.downloadTeamsImage(idTeam: teams[index].getId(), completion:
                    {(teamsImg) in self.teamsImg = teamsImg
                })
                let finalId : Int = teams[index].getId()
                let finalName : String = teams[index].getName()
                let finalMember : Int = teams[index].getMembers()
                var finalURL : URL = URL(string : "http://localhost/MyWebService/images/EmptyImg.png")!

                for index2 in (0...(self.teamsImg!.count - 1)){
                    if (finalId == self.teamsImg![index2].getId()){
                        finalURL = self.teamsImg![index2].getImagePath()
                    }
                }

                let tempCompleteTeam = JsonCompleteTeam(id: finalId, name: finalName, member: finalMember, imgUrl: finalURL)
                self.teamsComplete?.append(tempCompleteTeam)


            }

            //self.InsertNewTeamButton((Any).self)
            self.showTeamButton((Any).self)

        })
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // somethings in new wc //
    }


    @IBAction func showTeamButton(_ sender: Any) {
        performSegue(withIdentifier: "TeamListSegue", sender: self)
    }

    func downloadTeams(completion: @escaping (([JsonTeam]) -> Void)) {
        //get teams with classes
        let getTeamUrl = "http://192.168.178.77/MyWebService/api/getteams.php"

        guard let urlTeam = URL(string: getTeamUrl) else { return }
        URLSession.shared.dataTask(with: urlTeam) { (data,response,err) in
            guard let data = data else {return}
            do {
                let team = try JSONDecoder().decode(ClassJsonTeam.self, from: data)
                var tempArrayTeam = [JsonTeam]()
                for index in 0...(team.getTeams().count - 1) {

                    let tempTeam = JsonTeam(id: team.getTeams()[index].getId(),
                                            name: team.getTeams()[index].getName(),
                                            member: team.getTeams()[index].getMembers())

                    print(team.getTeams()[index].getId())
                    print(team.getTeams()[index].getName())
                    print(team.getTeams()[index].getMembers())

                    tempArrayTeam.append(tempTeam)
                }
                completion(tempArrayTeam)
            } catch let jsonErr{
                print("Error serializing json: \(jsonErr.localizedDescription)")
            }
            }.resume()
    }

    func downloadTeamsImage(idTeam: Int,completion: @escaping (([JsonTeamImg]) -> Void)) {
        //get teams with classes
        let getTeamUrl = "http://127.0.0.1/MyWebService/api/fetch_image.php?id_team=\(idTeam)"

        guard let urlTeam = URL(string: getTeamUrl) else { return }
        URLSession.shared.dataTask(with: urlTeam) { (data,response,err) in
            guard let data = data else {return}
            do {
                let team = try JSONDecoder().decode(ClassJsonTeamImage.self, from: data)
                var tempArrayTeamImg = [JsonTeamImg]()
                for index in 0...(team.getTeamsImg().count - 1) {

                    let tempTeamImg = JsonTeamImg(id: team.getTeamsImg()[index].getId(),
                                               imagePath: team.getTeamsImg()[index].getImagePath(),
                                               teamId: team.getTeamsImg()[index].getTeamId())

                    print(tempTeamImg.getId())
                    print(tempTeamImg.getImagePath())
                    print(tempTeamImg.getTeamId())

                    tempArrayTeamImg.append(tempTeamImg)
                }
                completion(tempArrayTeamImg)
            } catch let jsonErr{
                print("Error serializing json: \(jsonErr.localizedDescription)")
            }
            }.resume()
    }

}

让我们重点讲一下:如果我分别调用这两个函数,它们当然可以工作,但是我不知道如何将下载的数据合并到do-catch中(例如,在两个变量小组和在teamImg)中,这样我们就可以贡献变量teamComplete并在另一个视图控制器中传输数据。

预先感谢:D

1 个答案:

答案 0 :(得分:0)

更新您viewDidLoad

 override func viewDidLoad() {
        super.viewDidLoad()

        downloadTeams(completion: { (teams) in

            self.teams = teams

            for index in (0...(self.teams!.count - 1)){
                self.downloadTeamsImage(idTeam: teams[index].getId(), completion:
                    {(teamsImg) in self.teamsImg = teamsImg

                let finalId : Int = teams[index].getId()
                let finalName : String = teams[index].getName()
                let finalMember : Int = teams[index].getMembers()
                var finalURL : URL = URL(string : "http://localhost/MyWebService/images/EmptyImg.png")!

                for index2 in (0...(self.teamsImg!.count - 1)){
                    if (finalId == self.teamsImg![index2].getId()){
                        finalURL = self.teamsImg![index2].getImagePath()
                    }
                }

                let tempCompleteTeam = JsonCompleteTeam(id: finalId, name: finalName, member: finalMember, imgUrl: finalURL)
                self.teamsComplete?.append(tempCompleteTeam)

                // YOU CAN MOVE FORWARD NOW AS ALL THE DATA IS LOADED 
                if index == self.teams!.count - 1 {
                   self.showTeamButton((Any).self)
                }

             }
           })

        })
    }

这将消除崩溃,并在加载所有数据后调用showTeamButton