Have the following scenario. Basically I want to use the item binded sheet init and bind it to an optional property which will be set when we want to show the sheet. On the outside it looks perfectly normal but something interesting I am noticing is that the item property isn't getting deinitialized, which leads to a retain cycle, here is a sample:
class Item: Identifiable {
var dismiss: () -> Void
init(dismiss: @escaping () -> Void) {
self.dismiss = dismiss
print("init >> item \(Unmanaged.passUnretained(self).toOpaque())")
}
deinit {
print("deinit >> item \(Unmanaged.passUnretained(self).toOpaque())")
}
func start() -> some View {
ViewTwo(dismiss: dismiss)
}
}
struct ContentView: View {
@State private var item: Item? {
didSet {
print(item)
}
}
var body: some View {
Button("Show") {
item = Item {
item = nil
}
}
.sheet(item: $item) { item in
item.start()
}
}
}
struct ViewTwo: View {
var dismiss: () -> Void
var body: some View {
VStack {
Button("Dismiss") {
dismiss()
}
}
}
}