如何编辑我的函数并添加参数数组?

时间:2019-04-16 08:40:27

标签: arrays json swift

我具有Json解码功能,如何对函数进行现代化处理并添加参数,以数组类型输入“模型”数据?

我需要在所有全局数组中添加所有解码数据“模型”。 示例:

let myArray = [Any]()

在我认为添加的功能中

myArray.append(model.Welcome)

我的用于解码JSON的代码:

struct Welcome: Codable {
    let id: Int
    let name, username, email: String
    let address: Address
    let phone, website: String
    let company: Company
}

struct Address: Codable {
    let street, suite, city, zipcode: String
    let geo: Geo
}

struct Geo: Codable {
    let lat, lng: String
}

struct Company: Codable {
    let name, catchPhrase, bs: String
}


func userApiJsonCodableClass () {
    guard let url = URL(string: "https://jsonplaceholder.typicode.com/users") else {return}
    let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
        guard let dataResponse = data,
            error == nil else {
                print(error?.localizedDescription ?? "Response Error")
                return }

        do {
            //here dataResponse received from a network request
            let decoder = JSONDecoder()
            let model = try decoder.decode([Welcome].self, from:
                dataResponse) //Decode JSON Response Data
            print("---------CodableClass---------")
            print(model[0].website) //Output - 1221
        } catch let parsingError {
            print("Error", parsingError)
        }
    }
    task.resume()
}

2 个答案:

答案 0 :(得分:0)

首先在此处更改:var myArray = [Welcome]()。其次,将您的物品直接收集到一个数组中:myArray = try decoder.decode([Welcome].self, from: dataResponse) //Decode JSON Response Data

请不要忘记在解析错误时清除数组。

var myArray = [Welcome]()

struct Welcome: Codable {
    let id: Int
    let name, username, email: String
    let address: Address
    let phone, website: String
    let company: Company
}

struct Address: Codable {
    let street, suite, city, zipcode: String
    let geo: Geo
}

struct Geo: Codable {
    let lat, lng: String
}

struct Company: Codable {
    let name, catchPhrase, bs: String
}


    func userApiJsonCodableClass () {
        guard let url = URL(string: "https://jsonplaceholder.typicode.com/users") else {return}
        let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
            guard let dataResponse = data,
                error == nil else {
                    print(error?.localizedDescription ?? "Response Error")
                    return }

            do {
                //here dataResponse received from a network request
                let decoder = JSONDecoder()


                myArray = try decoder.decode([Welcome].self, from:
                    dataResponse) //Decode JSON Response Data



                print("---------CodableClass---------")
                print(myArray.first?.website) //Output - 1221
            } catch let parsingError {
                print("Error", parsingError)
                myArray = []
            }
        }
        task.resume()
    }

答案 1 :(得分:0)

以一种简单的方式,您可以将已解析的数组直接添加到全局数组"myArray"中,并将var用于myArray,例如:

var myArray = [Welcome]()

myArray = try decoder.decode([Welcome].self, from:
            dataResponse) //Decode JSON Response Data