一旦我在控制台中有了基础结果,如何将它们放入结构中?

Once I have the the foundation results in the console, How to put them in a struct?

我完成了从 JSON 中提取控制台中三个数组(名称、艺术家和价格)结果的整个过程。现在我需要将它们放在一个结构数组中,我将使用它来最终填充我的 tableView。我被困在这里。

我试图将控制台中的最终常量字符串分配为结构 属性 的参数。我想,然后我可以将它们用于 append 方法来实现我的结构数组。我在这里错过了一些东西。我不能填满我的数组。我没有在 Swift 3 中使用实际的 Codable 协议,因为我认为在攻击最后的 Swift 更新之前清楚地理解它对我的学习很重要。

做{ 如果让 json = 尝试 JSONSerialization.jsonObject(with: data, options: []) 作为? [字符串:任何] {

                let results = json as? [String: Any]
                if let feed = results!["feed"] as? [String: Any]{
                    if let entry = feed["entry"] as?  [[String: Any]]{
                        for item in entry{
                            if let price = item["im:price"] as? [String: Any]{
                                if let labelPrice = price["label"] as? String{
                                    print(labelPrice)
                                    self.topTen.songPrice = labelPrice
                                }
                            }
                        }
                        for item2 in entry{
                            if let name = item2["im:name"] as? [String: Any]{
                                if let labelName = name["label"] as? String{
                                    print(labelName)
                                    self.topTen.name = labelName
                                }
                            }
                        }
                        for item3 in entry{
                            if let artist = item3["im:artist"] as? [String: Any]{
                                if let labelArtist = artist["label"] as? String{
                                    print(labelArtist)
                                    self.topTen.artist = labelArtist
                                }
                            }
                        }
                    }
                }
                DispatchQueue.main.async {
                self.tableView.reloadData()
                }
            }
        }
        catch{
            print(error.localizedDescription)
        }

    }
    task.resume()
}

如果我插入:topTenArray.append((TopTen(name: topTen.name, artist: topTen.artist, songPrice: topTen.songPrice))) ,我无法填充数组。

您需要将结构对象创建到一个局部变量,然后将该对象追加到您的数组中,就像这样。并且不需要在同一个对象上进行多次循环。你可以像这样简单地做。

let results = json as? [String: Any]

                if let feed = results!["feed"] as? [String: Any] {

                    if let entry = feed["entry"] as?  [[String: Any]] {

                        for item in entry {

                            let topTen = YourTopTenStructure()

                            if let price = item["im:price"] as? [String: Any]{
                                if let labelPrice = price["label"] as? String{
                                    print(labelPrice)
                                    topTen.songPrice = labelPrice
                                }
                            }

                            if let name = item["im:name"] as? [String: Any]{
                                if let labelName = name["label"] as? String{
                                    print(labelName)
                                    topTen.name = labelName
                                }
                            }

                            if let artist = item["im:artist"] as? [String: Any]{
                                if let labelArtist = artist["label"] as? String{
                                    print(labelArtist)
                                    topTen.artist = labelArtist
                                }
                            }

                        topTenArray.append(topTen)

                       }
                    }
                }
                DispatchQueue.main.async {
                self.tableView.reloadData()
                }
            }
        }
        catch{
            print(error.localizedDescription)
        }

    }
    task.resume()