首页 > 解决方案 > 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)
   
        }
        
        
        
    }
}

标签: swiftswiftui

解决方案


问题不是因为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 中使用。


推荐阅读