Hello everyone, I just started creating my first app for iOS. I have a question for you. I am creating a List with results taken from a query on my personal database. Now I would like to pass the "f" parameter containing a date to the entire query. Would anyone know how to help me?
The error is: " cannot use instance member "f" within property initializer "
import SwiftUI
struct BarberView: View {
@Binding var f:String
var fetcher = MovieFetcher(f: f)
var body: some View {
VStack{
Text("\(f)")
List(fetcher.movies ) { movie in
VStack (alignment: .leading) {
Text(movie.data)
Text(movie.ora)
.font(.system(size: 11))
.foregroundColor(Color.gray)
Text(movie.barber)
Text(movie.user)
}
}
}
}
}
public class MovieFetcher: ObservableObject {
@Published var movies = [Movie]()
@Published var f:String
init(f: String){
self.f = f
load(f: f)
}
func load(f: String) {
let url = NSMutableURLRequest(url: NSURL(string: "http://familyverde.ddns.net/prova/checkapp.php")! as URL)
url.httpMethod = "POST"
let postString = "data=\(f)"
url.httpBody = postString.data(using: String.Encoding.utf8)
print("IL VALORE DI F: \(f)")
URLSession.shared.dataTask(with: url as URLRequest) {(data,response,error) in
do {
if let d = data {
let decodedLists = try JSONDecoder().decode([Movie].self, from: d)
DispatchQueue.main.async {
self.movies = decodedLists
}
}else {
print("No Data")
}
} catch {
print ("Error")
}
}.resume()
}
}
struct Movie: Codable, Identifiable {
public var id: String
public var data: String
public var ora: String
public var barber: String
public var user: String
enum CodingKeys: String, CodingKey {
case id = "id"
case data = "data"
case ora = "ora"
case barber = "barber"
case user = "user"
}
}
Selecting any option will automatically load the page