The Swift book says that "to prevent strong reference cycles, delegates are declared as weak references."
protocol SomeDelegate: AnyObject {
}
class viewController: UIViewController, SomeDelegate {
weak var delegate: SomeDelegate?
override func viewDidLoad() {
delegate = self
}
}
Say the class parameterizes a struct with that delegate
class viewController: UIViewController, SomeDelegate {
weak var delegate: SomeDelegate?
override func viewDidLoad() {
delegate = self
let exampleView = ExampleView(delegate: delegate)
let hostingController = UIHostingController(rootView: exampleView)
self.present(hostingController, animated: true)
}
}
struct ExampleView: View {
var delegate: SomeDelegate!
var body: some View {
Text("")
}
}
Should the delegate property in the struct also be marked with weak
?