-
Notifications
You must be signed in to change notification settings - Fork 156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Accessing properties on a view that takes a generic #294
Comments
Would casting the result of E.g., something like: let wrapperView = try foundWrapper.actualView() as? WrapperView<EmptyView>
let foundWrapperAV = wrapperView?.someProperty (I’m not in front of Xcode to try this out right now, so the code above might not be exact.) |
Late follow up, but this doesn't work. The actualView method still throws with a type mismatch. Which makes sense, as the view that is found is something like WrapperView<VStack<...>> |
This is a tricky one. Accessing There is a hacky way though. Try the following: @testable import ViewInspector // add @testable so you could use internal methods of ViewInspector
let foundWrapper = try sut.find(WrapperView<EmptyView>.self)
let value = try Inspector.attribute(path: "content|view|someProperty", value: foundWrapper, type: Int.self) |
Hey @nalexn ! I have a similar issue! Given a custom container, how can we inspect the generic property struct CustomContainer<Content: View>: View {
let content: Content
// ...
} I'm thinking, this should be possible right since VStack and HStack, etc have a very similar definition? // SwiftUI VStack
struct VStack<Content> : View where Content : View {} Also, is there anyway you could briefly explain the Thanks so much for your time! |
@josh-arnold-1 the internal function |
Thanks a lot for the context! How is the E.g, something like this? let view = AnyView(HStack { Text("abc") })
let text = try sut.inspect().find(text: "abc")
let hStack = try text.parent().hStack()
let anyView = try text.parent().parent().anyView() I'm wondering if the logic for VStack, for example, could somehow be generalized so it also works for custom generic containers like in my example? Like, I'm wondering what the difference is between these types?
Thanks! |
It also uses
struct CustomContainer<Content>: View where Content : View {}
struct VStack<Content> : View where Content : View {} The difference is that Custom view can have arbitrary inner structure, reference child views and arbitrary properties. If it's a property outside SwiftUI hierarchy, like |
Alternatively you can introduce a protocol without generics, like protocol MyCustomContainerView: View {
var contentView: Any { get }
} conform to that: |
Considering some view that takes in content
We can find a view that is nested in content like so:
But, now it seems as though it's impossible to access someProperty?
let foundWrapperAV = try foundWrapper.actualView().someProperty // fails with type mismatch
Just want to make sure we are not missing anything. we'd like to be able to get the properties of
WrapperView
, ignoring theContent
genericThe text was updated successfully, but these errors were encountered: