Ios 如何在swiftui中动态更改文本背景色?

Ios 如何在swiftui中动态更改文本背景色?,ios,swift,swiftui,Ios,Swift,Swiftui,我想通过传递函数将文本背景颜色更改为.background(),如下所示,颜色值为0或1或2将取决于DB的数据。如何修复它: import SwiftUI struct ContentView: View { @State var color = 0; var body: some View { Text("Hello, World!") .background(changeBkColor(int : self.$co

我想通过传递函数将文本背景颜色更改为.background(),如下所示,颜色值为0或1或2将取决于DB的数据。如何修复它:

import SwiftUI

struct ContentView: View {
    @State var color = 0;
    var body: some View {
        Text("Hello, World!")
            .background(changeBkColor(int : self.$color))
        
            
    }
}
func changeBkColor(int : color)
{
    if(color == 1)
    {
        return Color.red;
    }
    else if(color == 2)
    {
        return Color.green;
    }
    else
    {
        return Color.blue;
    }
}
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
给你

struct ContentView: View {
    @State var color = 0;
    var body: some View {
        Text("Hello, World!")
            .background(changeBkColor(color: self.color))


    }
}
func changeBkColor(color: Int) -> Color
{
    if(color == 1)
    {
        return Color.red;
    }
    else if(color == 2)
    {
        return Color.green;
    }
    else
    {
        return Color.blue;
    }
}