无法将类型 ‘Image’ 的值转换为预期的参数类型 ‘String’

huangapple go评论81阅读模式
英文:

Cannot convert value of type 'Image' to expected argument type 'String'

问题

I make a category included image scroll but I can't put my variable named tabBarItemName name into image, it keeps giving error like this: Cannot convert value of type 'Image' to expected argument type 'String'

struct ContentView: View {
    @State var currentTab: Int = 0
    var body: some View {
        ZStack(alignment: .top) {
            TabView(selection: self.$currentTab) {
                Image1View().tag(0)
                Image2View().tag(1)
                Image3View().tag(2)
            }
            .tabViewStyle(.page(indexDisplayMode: .never))
            .edgesIgnoringSafeArea(.all)
            .padding(.top, 76)
            CategoryScroll(currentTab: self.$currentTab)
        }
    }
}

struct CategoryScroll: View {
    var tabBarOptions: [String] = ["bim"]
    @Binding var currentTab: Int
    var body: some View {
        ScrollView(.horizontal) {
            HStack(spacing: 20) {
                ForEach(Array(zip(self.tabBarOptions.indices, self.tabBarOptions)),
                        id: \.0,
                        content: { index, name in
                    CategoryView(currentTab: self.$currentTab, tab: index, tabBarItemName: name)
                })
            }
        }
        .padding(7)
        .background(Color.white)
    }
}

struct CategoryView: View {
    @Binding var currentTab: Int
    var tab: Int
    var tabBarItemName: String
    var body: some View {
        Button {
            self.currentTab = tab
        } label: {
            VStack {
                Text(tabBarItemName) // Changes made here
                    .frame(width: 12, height: 12)
                    .padding(.leading, 65)

                if currentTab == tab {
                    Color.init(red: 0.965, green: 0.224, blue: 0.49)
                        .frame(height: 2)
                        .padding(.leading, 65)
                } else {
                    Color.clear.frame(height: 6)
                }
            }
            .animation(.spring(), value: self.currentTab)
        }
        .buttonStyle(.plain)
    }
}

I have made changes to your code to resolve the error. Specifically, I changed the tabBarItemName variable from Image type to String type in the CategoryView struct, and I updated the Image view to Text view in the CategoryView as well.

英文:

I make a category included image scroll butI can't put my variable named tabBarItemName name into image, it keeps giving error like this : Cannot convert value of type 'Image' to expected argument type 'String'

struct ContentView: View {
        @State var currentTab: Int = 0
        var body: some View {
            ZStack(alignment:.top) {
                        TabView(selection:self.$currentTab){
                            Image1View().tag(0)
                            Image2View().tag(1)
                            Image3View().tag(2)
                        }
                        .tabViewStyle(.page(indexDisplayMode: .never))
                        .edgesIgnoringSafeArea(.all)
                        .padding(.top,76)
                        CategoryScroll(currentTab: self.$currentTab)
                    }   
       }
    }

    struct CategoryScroll:  View {
        var tabBarOptions: [Image] = [Image("bim")]
        @Binding var currentTab : Int
        var body: some View {
              ScrollView(.horizontal){
                  HStack(spacing:20){
                      ForEach(Array(zip(self.tabBarOptions.indices, self.tabBarOptions)),
                              id:\.0,
                              content: {
                          index, name in
                          CategoryView(currentTab: self.$currentTab, tab: index, tabBarItemName: name)
              })
            }
        }.padding(7)
        .background(Color.white)
    }

    struct CategoryView: View {
        @Binding var currentTab: Int
        var tab : Int
        var tabBarItemName: Image
        var body: some View{
            Button{
                self.currentTab = tab
            } label: {
                VStack{
                    Image(tabBarItemName) // I'm trying to make changes right here
                        .frame(width: 12, height: 12)
                        .padding(.leading, 65)
    
                    if currentTab == tab {
                        Color.init( red: 0.965, green: 0.224, blue: 0.49)
                            .frame(height: 2)
                            .padding(.leading,65)
                    } else {
                        Color.clear.frame(height: 6)
                    }
                }
                .animation(.spring(), value: self.currentTab)
                
            }
           .buttonStyle(.plain)
        }
      }
    }

I would be very happy if you could help me with this.

答案1

得分: 0

将以下内容进行翻译:

var tabBarOptions: [Image] = [Image("bim")]

改为

var tabBarOptions: [String] = ["bim"]

并将

var tabBarItemName: Image

改为

var tabBarItemName: String

问题:

Image(tabBarItemName) 期望的是一个字符串,而不是一个图像。

英文:

Change

var tabBarOptions: [Image] = [Image("bim")]

to

var tabBarOptions: [String] = ["bim"]

And change

var tabBarItemName: Image

to

var tabBarItemName: String

The issue:

Image(tabBarItemName) is expecting a String, not an Image

huangapple
  • 本文由 发表于 2023年6月1日 21:42:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76382531.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定