0

Several Views were received as 'TurpleView<_VariadicView.Children>'.

struct View1: View {
    var body: some View {
        VStack {
            Text("page!")
        }
    }
}


struct Routable<Content: View> : View where Content : View {
    var activated:Bool = false
    let _content: Content
    
    init(@ViewBuilder content: @escaping() -> Content) {
        self._content = content()
    }
    var body: some View {
        _content
    }
}

struct SplashViews: View {
    var body: some View {
        ZStack {
            HStack {
                RouterStack {
                    Routable {
                        View1()
                    }
                    Routable {
                        View1()
                    }
                }
            }
        }
    }
}

And the handle was converted to Array format, However, AnyView cannot be converted to the original class.

public typealias Views = _VariadicView.Children

extension TupleView {
    var views: [AnyView] {
        make_array(from: value)
    }
    
    private struct GenericView {
        let body: Any
        
        var any_view: AnyView? {
            AnyView(_fromValue: body)
        }
    }
    
    private func make_array<Tuple>(from tuple: Tuple) -> [AnyView] {
        func convert(child: Mirror.Child) -> AnyView? {
            withUnsafeBytes(of: child.value) { pointer -> AnyView? in
                if case Optional<Any>.some(_) = child.value {
                    print("[TurpleView Children Value]: \(child)")
                    
                    let binded = pointer.bindMemory(to: GenericView.self)
                    return binded.first?.any_view
                } 
                
                return nil
            }
        }
        
        let mirror = Mirror(reflecting: tuple)
        return mirror.children.compactMap(convert)
    }
}

    struct RouterStack: View {
        @ObservedObject var _router: Router

        private let _content: [AnyView]
        
        init<Views>(@ViewBuilder content: @escaping() -> TupleView<Views>) {
            self._content = content().views
            self._router = Router(self._content)

            let x:AnyView = self._content[0] as Routable
            // print("\(x.activated)")
            // Cannot convert value of type 'AnyView' to type 'Routable<Content>' in coercion
            // Cannot convert value of type 'Routable<Content>' to specified type 'AnyView'
            // Generic parameter 'Content' could not be inferred in cast to 'Routable'
            
        }
        
        var body: some View {
            NavigationView {
             
            }
        }
    }

Is this possible?

I want to access the internal functions and variables of the view received through TurpleView. Or I don't have to use TurpleView, is there a better way?

3
  • Can you explain, at a high level, what you are trying to do? Why are you trying to get a Routable out of a @ViewBuilder? What are you going to do with it once you got it? This sounds very like an XY Problem.
    – Sweeper
    Commented Jul 10 at 1:59
  • My final result has multiple Views, The views will be displayed sequentially. View1 -> View 2 -> View 3 The condition for moving to the next view is, for example... when calling the next function. The reason for the above configuration is that the view can be skipped depending on conditions. View 1 -> View2(skip) -> View 3 == View 1-> View3 To create the skip logic, we asked the above questions.....Additionally, the skip condition can occur asynchronously.
    – OlderPeo
    Commented Jul 10 at 4:04
  • You still have not explained why you need to cast to Routable. I'd suggest that you take a step back, and edit your question to ask about your actual problem instead. Right now you seem to be going in a very wrong direction. Show, with code, an example of how someone might use your RouterStack (including how they would add an async skip condition). Describe how that code would behave.
    – Sweeper
    Commented Jul 10 at 4:12

0

Browse other questions tagged or ask your own question.