Let results = try decoder.decode(SearchResponse.self, from: data)

IOS Networking (2023) Module 1 Lesson 8

do {
                    let results = try decoder.decode(SearchResponse.self, from: data)
                    
                    for photo in results.photos {
                        print(photo)
                    }
                } catch {
                    print(error)
                }
                

why use this try decoder.decode(SearchResponse.self, from: data) instead of
try decoder.decode([SearchResponse].self, from: data)

Chris’s explanation is not clear. In IOS fundamental 2023, we use
try decoder.decode([SearchResponse].self, from: data)

Thanks.

@gotgoodjob

Which part of the course are you referring to? Module, Lesson and video timestamp?

If you use Proxyman you will see that the returned json from the api call is as follows assuming that the settings you have for the url enables on 1 page to be returned:
https://api.pexels.com/v1/search?query=nature&per_page=1

{
  "page": 1,
  "per_page": 1,
  "photos": [
    {
      "id": 772803,
      "width": 4000,
      "height": 2750,
      "url": "https://www.pexels.com/photo/snow-coated-rocky-mountains-772803/",
      "photographer": "Tyler Lastovich",
      "photographer_url": "https://www.pexels.com/@lastly",
      "photographer_id": 129858,
      "avg_color": "#818C98",
      "src": {
        "original": "https://images.pexels.com/photos/772803/pexels-photo-772803.jpeg",
        "large2x": "https://images.pexels.com/photos/772803/pexels-photo-772803.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940",
        "large": "https://images.pexels.com/photos/772803/pexels-photo-772803.jpeg?auto=compress&cs=tinysrgb&h=650&w=940",
        "medium": "https://images.pexels.com/photos/772803/pexels-photo-772803.jpeg?auto=compress&cs=tinysrgb&h=350",
        "small": "https://images.pexels.com/photos/772803/pexels-photo-772803.jpeg?auto=compress&cs=tinysrgb&h=130",
        "portrait": "https://images.pexels.com/photos/772803/pexels-photo-772803.jpeg?auto=compress&cs=tinysrgb&fit=crop&h=1200&w=800",
        "landscape": "https://images.pexels.com/photos/772803/pexels-photo-772803.jpeg?auto=compress&cs=tinysrgb&fit=crop&h=627&w=1200",
        "tiny": "https://images.pexels.com/photos/772803/pexels-photo-772803.jpeg?auto=compress&cs=tinysrgb&dpr=1&fit=crop&h=200&w=280"
      },
      "liked": false,
      "alt": "Snow-coated Rocky Mountains"
    }
  ],
  "total_results": 8000,
  "next_page": "https://api.pexels.com/v1/search/?page=2&per_page=1&query=nature"
}

The opening brace tells you that it is not an array at that point. The array starts at photos even though in this case there is only one photo. That’s why the code is:
let results = try decoder.decode(SearchResponse.self, from: data)

If you changed the number of pages to be retrieved to, say, 2 in your url
https://api.pexels.com/v1/search?query=nature&per_page=2

…you would see the json returned like this:

{
  "page": 1,
  "per_page": 2,
  "photos": [
    {
      "id": 772803,
      "width": 4000,
      "height": 2750,
      "url": "https://www.pexels.com/photo/snow-coated-rocky-mountains-772803/",
      "photographer": "Tyler Lastovich",
      "photographer_url": "https://www.pexels.com/@lastly",
      "photographer_id": 129858,
      "avg_color": "#818C98",
      "src": {
        "original": "https://images.pexels.com/photos/772803/pexels-photo-772803.jpeg",
        "large2x": "https://images.pexels.com/photos/772803/pexels-photo-772803.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940",
        "large": "https://images.pexels.com/photos/772803/pexels-photo-772803.jpeg?auto=compress&cs=tinysrgb&h=650&w=940",
        "medium": "https://images.pexels.com/photos/772803/pexels-photo-772803.jpeg?auto=compress&cs=tinysrgb&h=350",
        "small": "https://images.pexels.com/photos/772803/pexels-photo-772803.jpeg?auto=compress&cs=tinysrgb&h=130",
        "portrait": "https://images.pexels.com/photos/772803/pexels-photo-772803.jpeg?auto=compress&cs=tinysrgb&fit=crop&h=1200&w=800",
        "landscape": "https://images.pexels.com/photos/772803/pexels-photo-772803.jpeg?auto=compress&cs=tinysrgb&fit=crop&h=627&w=1200",
        "tiny": "https://images.pexels.com/photos/772803/pexels-photo-772803.jpeg?auto=compress&cs=tinysrgb&dpr=1&fit=crop&h=200&w=280"
      },
      "liked": false,
      "alt": "Snow-coated Rocky Mountains"
    },
    {
      "id": 3225517,
      "width": 4664,
      "height": 5830,
      "url": "https://www.pexels.com/photo/photo-of-stream-during-daytime-3225517/",
      "photographer": "Michael Block",
      "photographer_url": "https://www.pexels.com/@michael-block-1691617",
      "photographer_id": 1691617,
      "avg_color": "#6D7B7C",
      "src": {
        "original": "https://images.pexels.com/photos/3225517/pexels-photo-3225517.jpeg",
        "large2x": "https://images.pexels.com/photos/3225517/pexels-photo-3225517.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940",
        "large": "https://images.pexels.com/photos/3225517/pexels-photo-3225517.jpeg?auto=compress&cs=tinysrgb&h=650&w=940",
        "medium": "https://images.pexels.com/photos/3225517/pexels-photo-3225517.jpeg?auto=compress&cs=tinysrgb&h=350",
        "small": "https://images.pexels.com/photos/3225517/pexels-photo-3225517.jpeg?auto=compress&cs=tinysrgb&h=130",
        "portrait": "https://images.pexels.com/photos/3225517/pexels-photo-3225517.jpeg?auto=compress&cs=tinysrgb&fit=crop&h=1200&w=800",
        "landscape": "https://images.pexels.com/photos/3225517/pexels-photo-3225517.jpeg?auto=compress&cs=tinysrgb&fit=crop&h=627&w=1200",
        "tiny": "https://images.pexels.com/photos/3225517/pexels-photo-3225517.jpeg?auto=compress&cs=tinysrgb&dpr=1&fit=crop&h=200&w=280"
      },
      "liked": false,
      "alt": "Photo Of Stream During Daytime"
    }
  ],
  "total_results": 8000,
  "next_page": "https://api.pexels.com/v1/search/?page=2&per_page=2&query=nature"
}

I hope that helps.

I checked that even I set the per_page = 3, I should still use the following
let results = try decoder.decode(SearchResponse.self, from: data)

as there is only one response?

Yes, that’s correct. There is only one response but it will contain 3 sets of images in the photo array.