1

I know this isn't totally SwiftUI related but please bear with me.

Using this excellent video by Karin Prater https://www.youtube.com/watch?v=ZHK5TwKwcE4

I have built an app using the spaceX API here : https://api.spacexdata.com/v4/launches using the same techniques as Karin.

I want to mock some data for previews. But I am stuck by the complexity of the returned data.

struct Launch: Codable, Identifiable {
    
    let links: Links?
    let success: Bool?
    let details: String?
    let name, dateUTC: String?
    let dateUnix: Int?
    let dateLocal: String?
    let launchLibraryID: String?
    let id: String

    enum CodingKeys: String, CodingKey {
         case links
         case success,details
         case name
         case dateUTC = "date_utc"
         case dateUnix = "date_unix"
         case dateLocal = "date_local"
         case launchLibraryID = "launch_library_id"
         case id
     }

struct Links: Codable {
    let patch: Patch

    enum CodingKeys: String, CodingKey {
        case patch
        
    }
}

// MARK: - Patch
struct Patch: Codable {
    let small, large: String?
}

Some example data returned with debug/print

SpaceX_API_Demo.Launch(links: Optional(SpaceX_API_Demo.Links(patch: SpaceX_API_Demo.Patch(small: Optional("https://images2.imgbox.com/94/f2/NN6Ph45r_o.png"), large: Optional("https://images2.imgbox.com/5b/02/QcxHUb5V_o.png")))), success: Optional(false), details: Optional("Engine failure at 33 seconds and loss of vehicle"), name: Optional("FalconSat"), dateUTC: Optional("2006-03-24T22:30:00.000Z"), dateUnix: Optional(1143239400), dateLocal: Optional("2006-03-25T10:30:00+12:00"), launchLibraryID: nil, id: "5eb87cd9ffd86e000604b32a"), SpaceX_API_Demo.Launch(links:

func successState() -> Launch {
    
    let launch = Launch(links: <#T##Links?#>, success: true, details: "", name: "", dateUTC: "", dateUnix: 1234567, dateLocal: "", launchLibraryID: "", id: "")
    
}

I have no idea how to fill the initializer for Links ...

Thanks for any help!

Bhadresh Kathiriya
  • 3,147
  • 2
  • 21
  • 41
jat
  • 183
  • 3
  • 14

2 Answers2

1

Postman is a really helpful tool in these situations. When looking at the raw text in a web browser, it's almost impossible to tell what's what and not get bogged down in the complexity of JSON

in your example, you can take a look at what you have in the console of the Postman and begin implementing the keys.

ss of postman

Here, you can see you did not implement reddit and flicker keywords, so start with implementing these:

struct Links: Codable {
    let patch: Patch
    let reddit: Reddit
    let flickr: Flickr
}

struct Reddit : Codable {
   let campaign: String? // I am assuming that these are optional strings.
   let launch: String?
   let media : String?
   let recovery : String?
}

struct Flickr : Codable {
   let small : [String] // again, I AM ASSUMING it is a string (since it is empty in my console), but you have to learn what type these are by checking the documentation of your API
   let original : [String]
} 

Also, you don't always have to specify CodingKeys enum everytime you create codable struct, SwiftUI automatically creates it anyways.

grandsirr
  • 584
  • 4
  • 19
0

See below for how I managed to correctly initialze "launch"

func successState() -> Launch {
    
    let links = Links(patch: Patch(small: "", large: ""), reddit: Reddit(campaign: "", launch: "", media: "", recovery: ""), flickr: Flickr(small: ["",""], original: ["",""]))
    
    let launch = Launch (links: links, success: true, details: "", name: "", dateUTC: "", dateUnix: 1234567, dateLocal: "", launchLibraryID: "", id: "")
    
    return launch
    
}
jat
  • 183
  • 3
  • 14