swift - How we can define a State variable type Any?
问题描述
Update: This Method is just for learning Goal not using in real project!(Until new update)
Here I do get 2 issue with defining a State var as Any
type. As you know, we have to initialize it. For example in this code I have to initialize my State var with Image, which is not I want, the all point of choosing any as type was freedom of feeding the type, but State take this freedom from me with initialize it, and cherry on top, I have to force unwrapping for State to became usable for it!
So with this 2 issue (initialize+force unwrapping) about var type any with State, do we have ways to solve those issues?
@State var anyItem: Any = UIImage(named: "apple")!
Here my New Update of code with help of master's:
import SwiftUI
struct ContentView: View
{
@State var anyItem: Any?
@State var changedType: Bool = false
init() {
_anyItem = .init(initialValue: UIImage(named: "Apple"))
}
var body: some View {
if changedType == false
{
Image(uiImage: anyItem as! UIImage)
}
else
{
Text(anyItem as! String)
}
Text("Test")
.onTapGesture
{
chandType.toggle()
anyItem = "anyItem changed to Text! Kaboom!"
}
}
}
V2: Advanced Version
import SwiftUI
struct ContentView: View
{
@State var anyItem: Any?
enum anyItemType { case String, Image, Unknowen }
@State var choosenType: anyItemType = anyItemType.Unknowen
var body: some View {
ZStack
{
VStack
{
if choosenType == anyItemType.String
{
Text(anyItem as! String).bold()
}
else if choosenType == anyItemType.Image
{
Image(uiImage: anyItem as! UIImage).resizable().frame(width: 200, height: 200, alignment: .center).clipShape(Circle())
}
}
VStack
{
Spacer()
Button(choosenType == anyItemType.String ? "Change anyItem Type to Image" : "Change anyItem Type to String")
{
if choosenType == anyItemType.String
{
anyItem = UIImage(named: "Apple")
choosenType = anyItemType.Image
}
else
{
anyItem = "anyItem changed to Text! KABOOOM!"
choosenType = anyItemType.String
}
}.font(Font.body.bold())
}
.padding(.bottom, 100)
}
}
}
解决方案
问题不是因为Any
。这是因为这一行:
UIImage(named: "apple")
返回一个可选的( UIImage?
)。
如果你想anyItem
接受类型的值,UIImage?
你需要使它Any?
:
@State var anyItem: Any? = UIImage(named: "apple")
或为可选选项提供默认值:
@State var anyItem: Any = UIImage(named: "apple") ?? UIImage()
这是一个如何anyItem
使用不同类型进行初始化的示例(这仅用于演示,可能不应该在实际应用程序中使用):
struct ContentView: View {
@State var anyItem: Any?
init() {
_anyItem = .init(initialValue: UIImage(named: "apple"))
}
var body: some View {
Text("Test")
.onAppear {
anyItem = "test"
}
}
}
但正如 Asperi 已经建议的那样,这不是一个好主意,您通常希望避免Any
在 SwiftUI 中使用。
推荐阅读
- outlook - 将电子邮件更改为前一天阅读的 Outlook 规则
- python - GroupBy 和根据 1970 年之前的日期范围转换值
- python - great_expectations:expect_column_values_to_match_json_schema 不将 json 模式作为输入
- python - Pyspark 替代 withColumn/UDF 用于不可腌制的对象 (BucketedRandomProjectionLSHModel)
- drupal - Recapctha 和选择 js 在 Drupal 8 中的 webform ajax 提交时不加载
- go - 如何从 Looker Golang sdk 返回值
- r - ggplot2:更改 scale_color_viridis_c 的 Alpha 但不是图例
- javascript - 如何在我调用 api 路由的地方部署下一个应用程序
- python - 如何在 Pandas 中将日期列和相关列一起取消嵌套?
- python - 注释仅返回指定的分组值 - Django