I'm getting: Cannot find 'viewAllArtwork' in scope
my detail view:
import SwiftUI
struct ArtworkDetailView: View {
@StateObject var viewAllArtwork = ViewAllArtwork()
var artwork: Artwork
var body: some View {
Text(artwork.title)
}
}
struct ArtworkDetailView_Previews: PreviewProvider {
static var previews: some View {
ArtworkDetailView(artwork: viewAllArtwork.artworksData[0])
}
}
and my Artwork Views:
import SwiftUI
struct ArtworkView: View {
@StateObject var singleArtwork = SingleArtwork()
var body: some View {
NavigationView {
List {
ForEach(singleArtwork.artworksData, id: \.self) { artwork in
HStack(spacing: 8) {
URLImage(urlString: "https://melvynbiddulph.co.uk/images/gallery/"+artwork.file)
Text(artwork.title)
.bold()
if (artwork.size != nil) {
Text(artwork.size)
}
}
}
}
.navigationTitle("Single Artwork")
.onAppear {
singleArtwork.fetch()
}
}
}
}
struct ArtworkView_Previews: PreviewProvider {
static var previews: some View {
ArtworkView()
}
}
and to get the data for this:
import Foundation
import SwiftUI
class ViewAllArtwork: ObservableObject {
@Published var artworksData: [Artwork] = []
func fetch() {
guard let url = URL(string: "https://melvynbiddulph.co.uk/api/artwork") else {
return
}
let task = URLSession.shared.dataTask(with: url) { data, response, error in
guard let data = data, error == nil else {
return
}
//convert to JSON
do {
let artworksData = try JSONDecoder().decode([Artwork].self, from: data)
DispatchQueue.main.async {
self.artworksData = artworksData
}
} catch {
print(error)
}
}
task.resume()
}
}
Please help