mysql 视图条件
Let's take a look at this simple example:
让我们看一个简单的例子:
If we would want to hide a subtitle text based on some condition there are few options on how to do this.
如果我们想根据某些条件隐藏字幕文本,则如何执行此操作的选项很少。
The most straightforward is to add a State
variable and if
condition to replace it with the EmptyView
.
最简单的方法是添加一个State
变量, if
有条件if
其替换为EmptyView
。
It’s simple and easy to read but it becomes a bit messy when there are multiple optional blocks and more complex conditions.
它简单易读,但是当存在多个可选块和更复杂的条件时,它会变得有些混乱。
Let’s see how we can use ViewModifier to improve this piece of code. So let’s create one and wrap our condition in it’s body:
让我们看看如何使用ViewModifier改进这段代码。 因此,让我们创建一个并将我们的条件包装在它的主体中:
Please note that we are wrapping our view into the Group
view to avoid this opaque type error:
请注意,我们将视图包装到“ Group
视图中,以避免出现这种不透明的类型错误:
“Function declares an opaque return type, but has no return statements in its body from which to infer an underlying type”
“函数声明了不透明的返回类型,但是在其主体中没有用于从其推断基础类型的返回语句”
Of course, there is a couple of other ways, like wrapping it into AnyView
or using ViewBuilder
property wrapper. It’s up to you what way to choose.
当然,还有一些其他的方式,像包成 AnyView
或使用 ViewBuilder
属性包装。 由您决定选择哪种方式。
Now we can use our shiny new ViewBuilder to make our view optional:
现在,我们可以使用闪亮的新ViewBuilder将视图设为可选:
It already looks a bit nicer. Isn’t it?But, let’s add an extension to the View
so it’s even easier to use and read.
它看起来已经更好了。 不是吗?但是,让我们为View
添加一个扩展,以便更易于使用和阅读。
Here is how our final code will look like:
我们的最终代码如下所示:
You can find the final result for the Playground in this Gist on Github.
您可以在Github上的这个Gist中找到操场的最终结果。
I hope what I shared did help you!
希望我分享的内容对您有所帮助!
翻译自: https://medium.com/macoclock/conditional-views-in-swiftui-dc09c808bc30
mysql 视图条件