View
自体を lazy にすることで @autoclosure
相当にできるわけですね(本文読めませんでしたが)。onAppear
を一つにまとめたいところですが。 struct LazyView<Content: View>: View { private let content: () -> Content @State private var _body: Content? init(_ content: @escaping () -> Content) { self.content = content } @ViewBuilder var body: some View { if _body != nil { _body!.onAppear { self._body = self.content() } } else { EmptyView().onAppear { self._body = self.content() } } } }
LazyView
を挟めばちゃんと動きました。 struct ContentView: View { var body: some View { NavigationView { NavigationLink(destination: LazyView { SecondView() }) { Text("Open") } } } }
LazyView
の init
は @autoclosure
も付けた方がいいかな。