Search code examples
iosswiftjsondecoder

getting only one item from decoding json response iOS


I've a response from MapBox API. I want to store each Feature object of features array in a array then want to display from that array to table view.

{
type: "FeatureCollection",
query: [
    "u"
],
features: [{
        id: "country.19678805456372290",
        type: "Feature",
        place_type: [
            "country"
        ],
        relevance: 1,
        properties: {
            wikidata: "Q30",
            short_code: "us"
        },
        text: "United States",
        place_name: "United States",
        matching_text: "US",
        matching_place_name: "US",
        bbox: [
            -179.9,
            18.8163608007951,
            -66.8847646185949,
            71.4202919997506
        ],
        center: [
            -97.9222112121185,
            39.3812661305678
        ],
        geometry: {
            type: "Point",
            coordinates: [
                -97.9222112121185,
                39.3812661305678
            ]
        }
    },
    {
        id: "country.12405201072814600",
        type: "Feature",
        place_type: [
            "country"
        ],
        relevance: 1,
        properties: {
            wikidata: "Q145",
            short_code: "gb"
        },
        text: "United Kingdom",
        place_name: "United Kingdom",
        bbox: [
            -8.74974065661991,
            49.802416901086,
            1.86276379960989,
            60.9093517989553
        ],
        center: [
            -2.36966957036279,
            54.2379333607472
        ],
        geometry: {
            type: "Point",
            coordinates: [
                -2.36966957036279,
                54.2379333607472
            ]
        }
    },
    {
        id: "country.11702916565595680",
        type: "Feature",
        place_type: [
            "country"
        ],
        relevance: 1,
        properties: {
            wikidata: "Q878",
            short_code: "ae"
        },
        text: "United Arab Emirates",
        place_name: "United Arab Emirates",
        bbox: [
            51.4160146192147,
            22.6282410017159,
            56.4814183948386,
            26.094609499407
        ],
        center: [
            54.2561723713588,
            23.8520599823879
        ],
        geometry: {
            type: "Point",
            coordinates: [
                54.2561723713588,
                23.8520599823879
            ]
        }
    },
    {
        id: "country.11871993712476590",
        type: "Feature",
        place_type: [
            "country"
        ],
        relevance: 1,
        properties: {
            wikidata: "Q212",
            short_code: "ua"
        },
        text: "Ukraine",
        place_name: "Ukraine",
        bbox: [
            22.1375690033684,
            44.3152913001972,
            40.2255909904191,
            52.3793529890401
        ],
        center: [
            31.3202829593814,
            49.3227937844972
        ],
        geometry: {
            type: "Point",
            coordinates: [
                31.3202829593814,
                49.3227937844972
            ]
        }
    },
    {
        id: "country.9140805109496660",
        type: "Feature",
        place_type: [
            "country"
        ],
        relevance: 1,
        properties: {
            wikidata: "Q77",
            short_code: "uy"
        },
        text: "Uruguay",
        place_name: "Uruguay",
        matching_text: "URY",
        matching_place_name: "URY",
        bbox: [
            -58.4891219951353,
            -35.0552819973662,
            -53.1014000735687,
            -30.0855740544354
        ],
        center: [
            -56.012396238658,
            -32.7996455126793
        ],
        geometry: {
            type: "Point",
            coordinates: [
                -56.012396238658,
                -32.7996455126793
            ]
        }
    }
],
attribution: "NOTICE: © 2021 Mapbox and its suppliers. All rights reserved. Use of this data is subject to the Mapbox Terms of Service (https://www.mapbox.com/about/maps/). This response and the information it contains may not be retained. POI(s) provided by Foursquare."

}

So, I create a model like this -

 struct Response: Codable {
      var features: [Feature]
 }

 struct Feature: Codable {
      var id: String!
      var type: String?
      var matching_place_name: String?
      var place_name: String?
      var geometry: Geometry
      var center: [Double]
      var properties: Properties
 }

 struct Geometry: Codable {
      var type: String?
      var coordinates: [Double]
 }

 struct Properties: Codable {
      var address: String?
 }

And Write this code -

var suggestedPlacenames: NSMutableArray = []
func doShowSuggestion(usersQuery: String) {
    let urlString = "\(mapbox_api)\(usersQuery).json?access_token=\(mapbox_access_token)"
    let url = URL(string: urlString)
    let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
         guard let data = data, error == nil else {
            print("Error in URLSeesion")
            return
        }
        if let result = try? JSONDecoder().decode(Response.self, from: data) {
            self.suggestedPlacenames.add(result)
            print(self.suggestedPlacenames.count)
        } else {}
        

But by this I always get 1 length from self.suggestedPlacenames whatever the length of the response in features. How can I get each features object separately in self.suggestedPlacenames?


Solution

  • In your code :

    self.suggestedPlacenames.add(result)
    

    You add only one element to your array. You must append the an array.

    // declare as an array of features
    var suggestedPlacenames = [Feature]()
    
    self.suggestedPlacenames.append(contentsOf: result.features)
    

    or

    self.suggestedPlacenames += result.features