Search code examples
arraysjsonstructswift3

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


I did the whole process of extracting from JSON the results of three arrays(name, artist and price) that I have in the console. Now I need to put them in a struct-array that I will use to finally populate my tableView. I am stucked here.

I tried to assign the final constant string in the console as the arguments of the struct property. I thought, I could then use them to the append method to fulfill my struct-array. I missed something here. I can not fill my array. I am not using the actual Codable protocol in Swift 3 because I consider important for my learning to clearly understand it before attacking the last Swift updates.

do{ if let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] {

                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()
}

If I insert: topTenArray.append((TopTen(name: topTen.name, artist: topTen.artist, songPrice: topTen.songPrice))) , I can't fill the array either.


Solution

  • You need to create your structure object to a local variable and append that object into your array like this. And no need multiple loops on the same object. You could do simply like this.

    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()