1

I know how to fix this error, but my fix leads to another error.

The initial problem is the following

@Binding var report: Report

init(property: Property, report: Binding<Report>, saveAction: @escaping () -> Void) {
    self.property = property
    self.report = report // cannot assign value of type Binding<Report> to type Report
    self.data = SpacesData(property: property, report: report)
    self.saveAction = saveAction
}

If I change to the following

var report: Binding<Report>

init(property: Property, report: Binding<Report>, saveAction: @escaping () -> Void) {
    self.property = property
    self.report = report
    self.data = SpacesData(property: property, report: report)
    self.saveAction = saveAction
}

The error goes away but I can no longer call methods on report. So for instance the following

let image = report.loadCoverImage(for: property)

throws the error

Dynamic key path member lookup cannot refer to instance method 'loadCoverImage(for:)'

bobby123uk
  • 892
  • 4
  • 17
  • 3
    In your first block of code do `self._report = report`, which assigns the argument value of `Binding` type to the actual Binding property wrapper – New Dev Dec 24 '20 at 15:42
  • Thanks @NewDev would you like to put that as an official answer and I will accept it? Would be great to know what just happen. To me that underscore is random as hell. Black magic. – bobby123uk Dec 24 '20 at 16:01
  • Yeah, it's a compiler-synthesized property that contains the property wrapper type. The Swift documentation touches on it [here](https://docs.swift.org/swift-book/LanguageGuide/Properties.html#ID617). I would have provided it as an answer, but it's technically a duplicate question: https://stackoverflow.com/questions/56973959/swiftui-how-to-implement-a-custom-init-with-binding-variables – New Dev Dec 24 '20 at 16:07

0 Answers0