【问题标题】:SwiftUI misleading error: 'Int' is not convertible to 'CGFloat'SwiftUI 误导性错误:“Int”不可转换为“CGFloat”
【发布时间】:2019-11-22 22:28:23
【问题描述】:

在第 83 行 .offset(x: 150, y:7) 上,我不断收到“Int”不可转换为“CGFloat”。如果我将数字包装在 CGFloat() 中,则相同的错误只会出现在其他地方。我知道这与我程序中的控制流有关,但我无法确定在哪里。请帮忙。

这是我的 DateTracking.swift 屏幕代码(出现错误)。

struct DateTracking: View {

    @State var recipes: [Recipe] = getRecipes()
    @State var filterRecipesBy: String = ""
    @State private var searchTerm: String = ""
    @State var sortedNames: [Item] =  getItemsSortedByNames()
    @State var sortedDays: [Item] =  getItemsSortedByDaysLeft()
    @State var arraySelection: Int = 0
    @State var sortBySelected: Bool = false
    @State var recipesShown: Bool = false
    @State var buttonShown: Bool = true


    var body: some View {

        NavigationView{

            ZStack{


                VStack{

                    SearchBar(text: $searchTerm)



                    if(self.arraySelection == 0){
                        QGrid(sortedDays.filter {
                                    self.searchTerm.isEmpty ? true :
                                        $0.imageName.localizedCaseInsensitiveContains(self.searchTerm)
                                }, columns: 3) { item in


                                    Button(action: {
                                        self.filterRecipesBy = item.imageName
                                        recipesShown.toggle()
                                    }){
                                        ListItem(imageName: item.imageName, daysLeft: item.daysLeft, redBackground: item.redBackground )
                                    }



                                }
                                //.background(Color(red: 239 / 255, green: 239 / 255, blue: 239 / 255))
                            }

                    if(self.arraySelection == 1){
                        QGrid(sortedNames.filter {
                                    self.searchTerm.isEmpty ? true :
                                        $0.imageName.localizedCaseInsensitiveContains(self.searchTerm)
                                }, columns: 3) { item in

                                    ListItem(imageName: item.imageName, daysLeft: item.daysLeft, redBackground: item.redBackground )

                                }
                            //.background(Color(red: 239 / 255, green: 239 / 255, blue: 239 / 255))
                        }

                    }



                if (self.sortBySelected == true){
                    VStack{

                        Button(action: {
                            self.sortBySelected.toggle()
                        }) {
                            Text("Done")

                        }
                        .offset(x: 150, y: 7)

                        Picker(selection: self.$arraySelection, label:
                            Text("Picker Name")){
                            Text("Expiry Date").tag(0)
                            Text("Names").tag(1)

                        }
                        .labelsHidden()


                    }
                    .frame(width:375)
                    .background(Color(red: 239 / 255, green: 239 / 255, blue: 239 / 255))
                    .offset(x: 0, y: 140)


                }

                if (self.recipesShown == true){
                    VStack(alignment: .center, spacing: 15) {

                        HStack {
                            Button(action: {self.recipesShown.toggle()
                            }){
                                Cross()
                            }
                            .offset(x: -70, y: 11)
                            Text(verbatim: "Recipes You Can Cook")
                                .font(.headline)
                                .fontWeight(.bold)
                                .padding(.top)
                                .offset(x: -50, y: 5)
                        }


                        RecipesVertical(recipes: self.recipes.filter($0.ingredients.contains(filterRecipesBy)))
                            .padding(.leading)

                            //.offset(x: 0, y: 140)
                    }
                    .frame(width:375, height:310, alignment: .center)
                    .background(Color.white)

                }

               /* if(buttonShown == true){
                    Button(action: {self.recipesShown = true
                        self.buttonShown = false
                    }){
                        Image("floatingrecipebutton")
                        .resizable()
                            .frame(width: 85, height: 85)
                            .shadow(radius: 10)

                                   }
                                       .offset(x: 125, y:200)
                }
               */





            }
                .navigationBarTitle(Text("Date Tracking"))
                .navigationBarItems(trailing:

                HStack{

                    Button(action: {
                        self.sortBySelected.toggle()
                        }){
                        Text("Sort by")
                    }

                        }


            )


        }

    }

}

【问题讨论】:

  • 这是一个“为什么这段代码不起作用”的问题。它需要一个 MCVE。 (stackoverflow.com/help/minimal-reproducible-example) 这里有很多代码,它不会为响应者编译,因为它包含很多我们没有的类型和函数。将其减少到产生问题的最小代码(我怀疑当你这样做时,你也会找到答案)。

标签: ios swift xcode swiftui


【解决方案1】:

在 Swift 中,您不能在没有明确转换的情况下将 Int 分配给 CGFloat。

在这种情况下,您可以改为1.0,而不是写1。这样就行了。

【解决方案2】:

我相信你得到的错误代码在这里是模糊的,如果你尝试解决 GCFloat 那么 Xcode 会带来一些其他类似的问题。

问题可能出在你的语法上(曾经是我的问题),这是我分享的另一个 youtube 视频链接,其中实际问题不同,Xcode 显示类似的错误。

https://www.youtube.com/watch?v=aLSzgZtOpG8&list=PLMRqhzcHGw1Z-lZaaun3A3mV9PbEfHANI&index=8

在 22:30 左右播放视频,会出现类似的 GCFloat 错误,而实际问题出在 VStack。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-23
    • 1970-01-01
    • 1970-01-01
    • 2010-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-21
    相关资源
    最近更新 更多