[ad_1]
I am studying to create my first app right here and have watched a number of YouTube tutorials I am struggling to merge collectively. Any assistance is a lot appreciated. After years of tutorials in varied programming languages, I am lastly coding one thing and sticking with it!
I simply can not grasp what I have to do to the code under and what I have to placed on one other view to permit me to name Restaurant.latitude and Restaurant.longitude.
I’m simply making an attempt to learn the lat/lengthy’s of all of the Eating places within the listing on my MapScreen vs the DashboardScreen. These are each Baby views to a MainView which is the mum or dad view to all logged in customers.
DashboardScreen – please notice I edited this easier. Simply belief that my stacks and layouts work. merchandise.identify, merchandise.latitude, and merchandise.longitude all pull from my JSON and show within the view with out error. After I go to MapScreen, I need to have the ability to name these merchandise.lat/longs or Restaurant.lat/lengthy to consumer in my pins. I’ve all of that discovered, simply want the information accessible to be learn.
import SwiftUI
struct Restaurant: Decodable {
let id: String
let identify: String
let latitude: Double
let longitude: Double
}
struct DashboardScreen: View {
@State var eating places = [Restaurant]()
var physique: some View {
NavigationView {
RestaurantList
} // NavigationView
} // Finish Physique
func loadData() {
guard let url = URL(string: "https://jsonfile.com/restaurantList.json") else {
print("Invalid URL")
return
} // Load Knowledge
let request = URLRequest(url: url)
URLSession.shared.dataTask(with: request) {information, response, error in
if let information = information {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
if let decodedResponse = attempt?
decoder.decode([Restaurant].self, from: information) {
DispatchQueue.essential.async {
self.eating places = decodedResponse
}
return
}
}
print("Fetch failed: (error?.localizedDescription ?? "Unknown error")")
}.resume()
}
}
non-public extension DashboardScreen {
non-public var RestaurantList: some View {
Checklist {
ForEach(eating places, id:.id) { merchandise in
NavigationLink(vacation spot: particulars(merchandise: merchandise)) {
HStack {
Textual content(merchandise.identify)
HStack {
Textual content(merchandise.latitude)
Textual content(merchandise.longitude)
}
} // HStack
} // Label
} // ForEach
} // Checklist
.onAppear(carry out: loadData)
}
}
[ad_2]
