Saturday, November 23, 2024
HomeiOS Developmentios - Working button for the subsequent web page in API name...

ios – Working button for the subsequent web page in API name Swift

[ad_1]

I am gonna make an app for viewing comics from https://xkcd.com and I am utilizing their API.
In my iOS app I’ve two buttons, subsequent web page and prev web page. I need to click on subsequent to get to the subsequent web page and and so forth.

That is my API:

struct Comedian: Codable {
 var month: String
 var num: Int
 var hyperlink: String
 var 12 months: String
 var information: String
 var safe_title: String
 var transcript: String
 var alt: String
 var img: String
 var title: String
 var day: String
}

enum ApiError: Error {
 case dataIsNil
}


class ApiCall {
 
 var comicNumber = 300
 
 func getComic(completion: @escaping (Outcome<Comedian, Error>) -> ()) {
     guard let url = URL(string: "https://xkcd.com/(comicNumber)/data.0.json") else {return}
     URLSession.shared.dataTask(with: url) { information, _, error in
         if let error = error {
             print(error)
             completion(.failure(error))
             return
         }
         guard let information = information else {
             print("information is nil")
             completion(.failure(ApiError.dataIsNil))
             return
         }
         do {
             let comedian = attempt JSONDecoder().decode(Comedian.self, from: information)
             //                print(comedian)
             DispatchQueue.principal.async {
                 completion(.success(comedian))
             }
         } catch {
             print(error)
             completion(.failure(error))
         }
     }
     .resume()
 }
}

And that is my view

    struct ComicContainer: View {
    
    @State var comedian: Comedian?
    
    var physique: some View {
        
        VStack {
            
            NavigationLink(vacation spot: ComicDetailView(), label: {
                AsyncImage(url: URL(string: comedian?.img ?? "Picture"))
                
            })
            Textual content("Comedian num: (comedian?.num ?? 0)")
                .padding()
                .onAppear {
                    ApiCall().getComic{ end in
                        change consequence {
                        case .success(let comedian):
                            self.comedian = comedian
                        case .failure(let error):
                            print(error)
                        }
                    }
                }
                .toolbar {
                    ToolbarItemGroup(placement: .bottomBar) {
                        CustomButton {
                            print("prev")
                        } content material: {
                            Textual content("Prev")
                        }
                        Spacer()
                        CustomButton {
                            print("subsequent")
                            
                        } content material: {
                            Textual content("Subsequent")
                        }
                        
                    }
                }
                .navigationBarTitle("(comedian?.title ?? "title")")
        }
    }
}

Does anybody have any tricks to how I can clear up this? I’m fairly new with swift and have been combating this.

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments