gpt4 book ai didi

Is AnyShape similar to AnyView, in the sense that I should avoid using it?(AnyShape类似于AnyView吗,就我应该避免使用它的意义而言?)

转载 作者:bug小助手 更新时间:2023-10-24 17:10:50 27 4
gpt4 key购买 nike



Many say that AnyView should be avoided when possible, because it can greatly affect the performance of the diffing that SwiftUI does. (1, 2) I understand why that is the case - the types of the views are erased, so SwiftUI cannot easily analyse the structure of the view.

许多人说,只要有可能,就应该避免使用AnyView,因为它会极大地影响SwiftUI所做的差异化的性能。(1,2)我理解为什么会这样--视图的类型被删除了,所以SwiftUI不能很容易地分析视图的结构。


There is also AnyShape, which is also a type eraser like AnyView. It erases type information, so I wonder if I should also avoid using it. From this answer, it seems like we can easily avoid it by writing our own wrapper something like this:

还有AnyShape,它和AnyView一样也是橡皮擦。它会删除类型信息,所以我想知道我是否也应该避免使用它。从这个答案来看,我们似乎可以通过编写自己的包装器来轻松地避免它:


struct CustomShape: Shape {
init<S: Shape>(_ wrapped: S) {
_path = { rect in
let path = wrapped.path(in: rect)
return path
}
}

func path(in rect: CGRect) -> Path {
return _path(rect)
}

private let _path: (CGRect) -> Path
}

There is also the possibility that the above is basically how the built-in AnyShape is implemented . Before AnyShape is available, many people were writing their own wrappers like this, so it is possible that Apple just added it as a built-in type.

也有可能上述内容基本上就是内置AnyShape的实现方式。在AnyShape面世之前,很多人都在像这样编写自己的包装器,所以有可能苹果只是将其添加为内置类型。


In any case, people still use AnyShape in a lot of situations. (1, 2) It seems to me like AnyShape isn't being avoided by people as much as AnyView is.

无论如何,人们在很多情况下仍然使用AnyShape。(1,2)在我看来,AnyShape并没有像AnyView那样受到人们的回避。


Why not? Doesn't SwitUI need to know about the specific types of shapes? Is it just because shapes are much simpler things than Views?

为什么不行?SwitUI不需要知道特定类型的形状吗?仅仅是因为形状比视图简单得多吗?


更多回答
优秀答案推荐
更多回答

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com