I am passing an argument locationPins into MyMapView as a Binding and then again pass locationPins to MapView as another Binding. But when I pass it to MapView as an argument I get error: "Argument passed to call that takes no arguments"
struct MyMapView: View {
@Binding var locationPins: [GpsData.MyEndPt]
var body: some View {
TabView {
NavigationView {
ZStack {
VStack (alignment: .leading){
MapView(locationPins: locationPins) // <<----- ERROR HERE
.ignoresSafeArea()
}
}
.safeAreaInset(edge: .bottom) {
Color.clear
.frame(height: 0)
.background(.white)
}
}
}
}
struct MapView: UIViewRepresentable {
@Binding var locationPins: [GpsData.MyEndPt]
@State var mylocationPins: [GpsData.MyEndPt] = []
init() {
self.mylocationPins = toPins(locationPins: self.locationPins)
}
func toPins(locationPins: [GpsData.MyEndPt]) -> [GpsData.MyEndPt] {
for location in locationPins {
// use locationPins to populate myLocationPins and return
}
return mylocationPins
}
}
If I remove the Binding definition of locationPins inside MapView, then I cannot pass the value.
How to fix this?
init()
with no parameters. If you want to initialize with parameters, you must declare them in the initializer's signatureinit
to set a@State
variable based on what's in aBinding
. I would strongly urge refactoring this to use the typicalmakeUIView
andupdateNSView
pattern and not do anything custom ininit
. Then, just remove yourinit
and let the compiler synthesize the initializer and you'll be fine