It still saying " ReadData() doesn’t find in scope " please I need help to fix this
ContentView…
import SwiftUI
struct ContentView: View {
@ObservedObject var datas = ReadData()
var body: some View {
List(datas.users){ user in
VStack(alignment: .leading) {
Text(user.name)
.font(.title)
.fontWeight(.heavy)
.foregroundColor(Color.gray)
HStack{
Text(user.designation)
.font(.title3)
.foregroundColor(Color.red)
Spacer()
Text(user.email)
.font(.title3)
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
New file:…
import Foundation
struct User: Codable, Identifiable {
enum CodingKeys: CodingKey {
case name
case designation
case email
}
var id = UUID()
var name: String
var designation: String
var email: String
}
class ReadData: ObservableObject {
@Published var users = User
init(){
loadData()
}
func loadData() {
guard let url = Bundle.main.url(forResource: "Userdata", withExtension: "json")
else {
print("Json file not found")
return
}
let data = try? Data(contentsOf: url)
let users = try? JSONDecoder().decode([User].self, from: data!)
self.users = users!
}
}