I am using Swift UI tutorial and now I am on Customize the Row Preview. This is my code:
LandmarkRow(landmark: landmarks[1])
I am getting an error "Could not find 'landmarks' in scope. Any suggestions?
Thanks!
Code Block let landmarkData: [Landmark] = load("landmarkData.json")
Code Block LandmarkRow(landmark: landmarkData[0])
It was better if you could clarify which tutorial you are using.I am using Swift UI tutorial
Code Block var landmarks: [Landmark] = load("landmarkData.json")
If you follow the tutorial but do not move Landmark.swift into the new Model group, XCode may not recognize that Landmark.swift is part of the project. Moving Landmark.swift into the Model group will fix the problem if that was the cause.
So what's the answer? I still can't figure it out
add this code
var landmarks: [Landmark] = load("landmarkData.json")
inside of modeldata.swift
Adding the var landmarks: [Landmark] = load("landmarkData.json") line to the modeldata.swift works, but doesn't this defeat the purpose of using a variable filename (which should come from the main bundle) in the load function? I am new to Swift so I may be missing something here.
Hey, I had exactly the same problem as you mentioned in the question. Note that in the Landmark.swift file you did everything as in the manual and wrote correctly For more bits delete the same code in the Landmark.swift file and copy the following code
import Foundation import SwiftUI import CoreLocation
struct Landmark: Hashable, Codable { var id: Int var name: String var park: String var state: String var description: String
private var imageName: String
var image: Image {
Image(imageName)
}
private var coordinates: Coordinates
var locationCoordinate: CLLocationCoordinate2D {
CLLocationCoordinate2D(
latitude: coordinates.latitude,
longitude: coordinates.longitude)
}
struct Coordinates: Hashable, Codable {
var latitude: Double
var longitude: Double
}
}
I've stumbled upon this problem during learning process and found a solution. In the Code section of the tutorial there is a line missing in the LandmarkRow_Previews function:
static var landmarks = ModelData().landmarks
Which is actually pretty crucial :) After adding, landmarks are in scope of this function.
let land