swift ui magic

struct TestView: View {
    var body: some View {
        var descriptionShape: any Shape = Circle()
        descriptionShape = Circle()
        
        return Text("description")
            .clipShape(descriptionShape)
    }
}

Why in the .clipShape(descriptionShape) occurs Type 'any View' cannot conform to 'View' error?

Accepted Reply

There's no entirely satisfactory answer I can give you here, without knowing more about the larger context of your actual code.

You can solve the immediate problem by type-erasing the shape:

struct TestView: View {
    var body: some View {
        var descriptionShape = AnyShape(Circle())
        
        return Text("description")
            .clipShape(descriptionShape)
    }
}

Note the difference here: AnyShape is a concrete type, but it is "type erased" so it can wrap any kind of Shape. It does also conform to Shape. any Shape is an existential type that does not conform to Shape.

However, this is pretty much a brute force approach., and may not perform well if there are frequent View updates.

One alternative would be to make your choice of shape initially as (for example) one of several enum cases, rather than an actual Shape. That gives you the flexibility to manipulate the choice of shape without crossing into existential-land. At the end of the View body, you'd then use (for example) a switch statement to choose the correct return value.

This is likely a bit more code, but is going to perform better than using type erasure,

  • Awesome! That's what I'm looking for, thank you so much!

Add a Comment

Replies

The problem arises from declaring descriptionSape as any Shape. That gives it an existential (protocol) type, not a concrete type.

OTOH, clipShape requires a concrete View type, but the compiler has no inference path leading from any Shape to any View to View.

How you would get around this depends what you're actually trying to do in your actual code. In this simplified code fragment, you could simply leave the type annotation off the declaration of descriptionShape, but probably doesn't solve the problem you started with.

  • yup, in general, I want to get a variable of shape that will be passed in the clipShape function, but I don't know which form I will have, that's why I'm trying to create an existential variable which can store any shape

Add a Comment

here is a solution:

struct TestView: View {
    var body: some View {
        Text("description")
            .clipShape(getShape(position: .middle))
    }
}

struct TestView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

enum ElementPosition {
    case first
    case middle
}

func getShape(position: ElementPosition) -> some Shape {
    switch position {
    case .first:
        return AnyShape(Circle())
    case .middle:
        return AnyShape(Rectangle())
    }
}

There's no entirely satisfactory answer I can give you here, without knowing more about the larger context of your actual code.

You can solve the immediate problem by type-erasing the shape:

struct TestView: View {
    var body: some View {
        var descriptionShape = AnyShape(Circle())
        
        return Text("description")
            .clipShape(descriptionShape)
    }
}

Note the difference here: AnyShape is a concrete type, but it is "type erased" so it can wrap any kind of Shape. It does also conform to Shape. any Shape is an existential type that does not conform to Shape.

However, this is pretty much a brute force approach., and may not perform well if there are frequent View updates.

One alternative would be to make your choice of shape initially as (for example) one of several enum cases, rather than an actual Shape. That gives you the flexibility to manipulate the choice of shape without crossing into existential-land. At the end of the View body, you'd then use (for example) a switch statement to choose the correct return value.

This is likely a bit more code, but is going to perform better than using type erasure,

  • Awesome! That's what I'm looking for, thank you so much!

Add a Comment