UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Download from internet failed.

Forums > SwiftUI

I've got a program that trys to download the picture of the day from NASA. I get download failed, but I know the URL works - here is the output I get

2023-01-27 12:27:18.587287+0000 NASA_APOD[19841:506259] [SceneConfiguration] Info.plist contained no UIScene configuration dictionary (looking for configuration named "(no name)")
2023-01-27 12:27:18.587452+0000 NASA_APOD[19841:506259] [SceneConfiguration] Info.plist contained no UIScene configuration dictionary (looking for configuration named "(no name)")
2023-01-27 12:27:18.587626+0000 NASA_APOD[19841:506259] [SceneConfiguration] Info.plist contained no UIScene configuration dictionary (looking for configuration named "(no name)")
download failed

Any ideas what it all means?

Here is ContentView.swift

//
//  ContentView.swift
//  NASA_APOD
//
//  Created by Gary Watson on 26/01/2023.
//

import SwiftUI

struct ContentView: View {
    @State private var pictures = [Picture]()

    var body: some View {
        List(pictures) { picture in
            NavigationLink {
                Text(picture.title)
            } label: {
                Text(picture.date)
            }
        }
        .task {
            await fetchMessages()
        }
    }

    func fetchMessages() async {
        guard pictures.isEmpty else { return }

        do {
            let url = URL(string: "https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY")!
            let (data, _) = try await URLSession.shared.data(from: url)

            let decoder = JSONDecoder()
            pictures = try decoder.decode([Picture].self, from: data)
        } catch {
            pictures = []
            print("download failed")
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

and here is Picture.swift

//
//  Picture.swift
//  NASA_APOD
//
//  Created by Gary Watson on 26/01/2023.
//

import Foundation
struct Picture: Identifiable, Codable {
    let id: UUID
    let copyright: String?
    let date: String
    let explanation: String
    let hdurl: String
    let media_type: String
    let service_version: String
    let title: String
    let url: String

    static let example = Picture(id: UUID(), copyright: "Dan Bartlett",
                                 date: "2023-01-27",
                                 explanation: "The current darling of the northern night, Comet C/2022 E3 ZTF is captured in this telescopic image from a dark sky location at June Lake, California. Of course Comet ZTF has been growing brighter in recent days, headed for its closest approach to Earth on February 1.  But this view was recorded on January 23, very close to the time planet Earth crossed the orbital plane of long-period Comet ZTF. The comet's broad, whitish dust tail is still curved and fanned out away from the Sun as Comet ZTF sweeps along its orbit. Due to perspective near the orbital plane crossing, components of the fanned out dust tail appear on both sides of the comet's green tinted coma though, to lend Comet ZTF a visually striking (left) anti-tail. Buffeted by solar activity the comet's narrower ion tail also streams away from the coma diagonally to the right, across the nearly three degree wide field of view.",
                                 hdurl: "https://apod.nasa.gov/apod/image/2301/C2022E3ZTF_2023_01_23_054036PST_DEBartlett.jpg",
                                 media_type: "image",
                                 service_version: "v1",
                                 title: "Comet ZTF: Orbital Plane Crossing",
                                 url: "https://apod.nasa.gov/apod/image/2301/C2022E3ZTF_2023_01_23_054036PST_DEBartlett1024.jpg")
}

2      

If I just put the url string into sarafi it returns some valid JSON.

I think the error is that I am expecting an array of records to be returned, but the API only ever returns a single record. Having said that I am unsure how to fix it.

2      

Change:

pictures = try decoder.decode([Picture].self, from: data)

to:

let dailyPic = try decoder.decode(Picture.self, from: data)
pictures = [dailyPic]

2      

thanks Rooster, turns out there were a few other things wrong, but you certainly put me on the right track.

2      

PS this warning

2023-01-27 12:27:18.587287+0000 NASA_APOD[19841:506259] [SceneConfiguration] Info.plist contained no UIScene configuration dictionary (looking for configuration named "(no name)

can be silenced by going to the Info tab on Targets then under Application Scene Manifest add row Scene Configuration

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.