I wat to show data from db in picker. i made it work if I use "data",now I want to change "data" to "data1",
then i get the error:"Cannot use instance member 'store' within property initializer; property initializers run before 'self' is available"
at line 09.
how to resole it?
here is my code:
Userlist.swift
import Foundation
import Foundation
import Combine
class Userlist: ObservableObject {
@Published var mainlist: [Mainlist] = []
init() {
load()
}
func load() {
let url = URL(string: "http://localhost:3000/name_v")!
URLSession.shared.dataTask(with: url) { data, response, error in
DispatchQueue.main.async {
self.mainlist = try! JSONDecoder().decode([Mainlist].self, from: data!)
}
}.resume()
}
}
struct Mainlist: Decodable{
let name: String
}
ContentView.swift
import SwiftUI
struct ContentView: View {
@ObservedObject var store = Userlist()
@State var selectedStrength = [""]
// var data: [String] = ["str1", "str2", "str3", "str4"]
var data1: [Mainlist] = store.mainlist
var body: some View {
singlePicke(data: data,selectedStrength: $selectedStrength)
}
}
struct singlePicke: View{
let data: [String]
@Binding var selectedStrength: [String]
var body: some View{
GeometryReader{Geometry in
Picker("picker",selection:self.$selectedStrength[0]){
ForEach(00..<self.data.count){raw in<br=""> Text(verbatim:self.data[raw]).tag(self.data[raw])
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}