OStack程序员社区-中国程序员成长平台

标题: ios - 使用 JSONDecoder 后如何正确调用属性 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 20:22
标题: ios - 使用 JSONDecoder 后如何正确调用属性

我正在解码 JSON,然后我创建了一个 completionHandler 函数,以便能够在项目中的任何位置获取 JSON 解析的属性。但是,当我调用该函数并只打印我想要的属性时,我将它们全部放在一起而不是调用的属性。

例如:我调用 beer.name,所以当我打印出来时,我得到了解码后的 Struct 中包含的所有属性和值。我究竟做错了什么?我需要将这些值分开,以便能够填充 UITableView 和其他组件。

View Controller

 override func viewDidLoad() {
        super.viewDidLoad()

        getApiData { (cerveja) in
            for beer in cerveja {
                print(beer.name!)
            } 
        }
    }

That's what I got for this call below:

[HakerRank___Bruno_Vavretchek.Cerveja(name: Optional("Buzz"), abv: 4.5, image_url: "https://images.punkapi.com/v2/keg.png"), HakerRank___Bruno_Vavretchek.Cerveja(name: Optional("Trashy Blonde"), abv: 4.1, image_url: "https://images.punkapi.com/v2/2.png"), HakerRank___Bruno_Vavretchek.Cerveja(name: Optional("Berliner Weisse With Yuzu - B-Sides"), abv: 4.2, image_url: "https://images.punkapi.com/v2/keg.png"), HakerRank___Bruno_Vavretchek.Cerveja(name: Optional("ilsen Lager"), abv: 6.3, image_url: "https://images.punkapi.com/v2/4.png"), HakerRank___Bruno_Vavretchek.Cerveja(name: Optional("Avery Brown Dredge"), abv: 7.2, image_url: "https://images.punkapi.com/v2/5.png"), HakerRank___Bruno_Vavretchek.Cerveja(name: Optional("Electric India"), abv: 5.2, image_url: "https://images.punkapi.com/v2/6.png"), HakerRank___Bruno_Vavretchek.Cerveja(name: Optional("AB:12"), abv: 11.2, image_url: "https://images.punkapi.com/v2/7.png"), HakerRank___Bruno_Vavretchek.Cerveja(name: Optional("Fake Lager"), abv: 4.7, image_url: "https://images.punkapi.com/v2/8.png"), HakerRank___Bruno_Vavretchek.Cerveja(name: Optional("AB:07"), abv: 12.5, image_url: "https://images.punkapi.com/v2/9.png"), HakerRank___Bruno_Vavretchek.Cerveja(name: Optional("Bramling X"), abv: 7.5, image_url: "https://images.punkapi.com/v2/10.png"), HakerRank___Bruno_Vavretchek.Cerveja(name: Optional("Misspent Youth"), abv: 7.3, image_url: "https://images.punkapi.com/v2/keg.png"), HakerRank___Bruno_Vavretchek.Cerveja(name: Optional("Arcade Nation"), abv: 5.3, image_url: "https://images.punkapi.com/v2/12.png"), HakerRank___Bruno_Vavretchek.Cerveja(name: Optional("Movember"), abv: 4.5, image_url: "https://images.punkapi.com/v2/13.png"), HakerRank___Bruno_Vavretchek.Cerveja(name: Optional("Alpha Dog"), abv: 4.5, image_url: "https://images.punkapi.com/v2/14.png"), HakerRank___Bruno_Vavretchek.Cerveja(name: Optional("Mixtape 8"), abv: 14.5, image_url: "https://images.punkapi.com/v2/15.png"), HakerRank___Bruno_Vavretchek.Cerveja(name: Optional("Libertine Porter"), abv: 6.1, image_url: "https://images.punkapi.com/v2/16.png"), HakerRank___Bruno_Vavretchek.Cerveja(name: Optional("AB:06"), abv: 11.2, image_url: "https://images.punkapi.com/v2/17.png"), HakerRank___Bruno_Vavretchek.Cerveja(name: Optional("Russian Doll – India Pale Ale"), abv: 6.0, image_url: "https://images.punkapi.com/v2/18.png"), HakerRank___Bruno_Vavretchek.Cerveja(name: Optional("Hello My Name Is Mette-Marit"), abv: 8.2, image_url: "https://images.punkapi.com/v2/19.png"), HakerRank___Bruno_Vavretchek.Cerveja(name: Optional("Rabiator"), abv: 10.27, image_url: "https://images.punkapi.com/v2/keg.png"), HakerRank___Bruno_Vavretchek.Cerveja(name: Optional("Vice Bier"), abv: 4.3, image_url: "https://images.punkapi.com/v2/keg.png"), HakerRank___Bruno_Vavretchek.Cerveja(name: Optional("Devine Rebel (w/ Mikkeller)"), abv: 12.5, image_url: "https://images.punkapi.com/v2/22.png"), HakerRank___Bruno_Vavretchek.Cerveja(name: Optional("Storm"), abv: 8.0, image_url: "https://images.punkapi.com/v2/23.png"), HakerRank___Bruno_Vavretchek.Cerveja(name: Optional("The End Of History"), abv: 55.0, image_url: "https://images.punkapi.com/v2/24.png"), HakerRank___Bruno_Vavretchek.Cerveja(name: Optional("Bad Pixie"), abv: 4.7, image_url: "https://images.punkapi.com/v2/25.png")]

我确实需要的是所有名称或 abv 或 image_url 的

模型文件夹:

struct Cervejaecodable{
    let name:String?
    let abvouble
    let image_url:String
}

网络文件夹:

func getApiData(completion: @escaping ([Cerveja]) -> ()){
    guard let urlString = URL(string: "https://api.punkapi.com/v2/beers") else {
        print("URL Error")
        return
    }
    Alamofire.request(urlString).responseJSON { response in

        if response.data == response.data{
            do{
                let decoder = try JSONDecoder().decode([Cerveja].self, from: response.data!)
                print(decoder)
            }catch{
        print(error)
            }
        }else{print("API Response is Empty")}

        }
}



Best Answer-推荐答案


实际上,您正在打印“解码器”。你必须这样写:

func getApiData(completion: @escaping ([Cerveja]) -> ()){
guard let urlString = URL(string: "https://api.punkapi.com/v2/beers") else {
    print("URL Error")
    return
}
Alamofire.request(urlString).responseJSON { response in

    if let data = response.data{
        do{
            let decoder = try JSONDecoder().decode([Cerveja].self, from: data)
            completion(decoder)
        }catch{
    print(error)
        }
    }else{print("API Response is Empty")}

    }

}

避免你强制解包。所以用

if let data = response.data{ 
   // Some code
}

然后就可以在viewDidLoad()中打印了

 override func viewDidLoad() {
    super.viewDidLoad()

    getApiData { (cerveja) in
        for beer in cerveja {
            print(beer.name!)
        } 
    }
}

关于ios - 使用 JSONDecoder 后如何正确调用属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51387145/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4