SwiftUI错误代码“无法推断通用参数'C0'”

时间:2019-12-30 13:32:16

标签: swift swiftui

我一直在处理一些SUI代码,但不断收到此错误:

Generic parameter 'C0' could not be inferred

这是它的摘录。我一直在不停地在网上狩猎,但找不到任何具体的东西。文件中的其他地方似乎没有任何语法错误,我已经尝试了几次经典的“断断续续”的技巧。

import SwiftUI

struct ContentView: View {

    var eliminationModel = EliminationModel()
    @State private var code: String = "0000"
    let roboto = UIFont(name: "Roboto Mono", size: UIFont.systemFontSize)


    var body: some View {
        ZStack() {
            Image("background")
                .resizable()
                .edgesIgnoringSafeArea(/*@START_MENU_TOKEN@*/.all/*@END_MENU_TOKEN@*/)
            VStack() {
                HStack() {
                    VStack() {
                        Text("CODE:")
                            .font(Font.custom("Roboto Mono", size: 15))
                            .multilineTextAlignment(.center)
                        Text("6969")
                            .font(Font.custom("Roboto Mono", size: 10))
                            .padding(.top, 10.0)

                        Button(action: {
                            EliminationModel.printTest()
                        }) {
                            Text("Button")
                        }
                    }

                    .padding(.horizontal, CGFloat(50.0))

                    VStack() {

                        .textFieldStyle(RoundedBorderTextFieldStyle())
                        Text("INPUT: \(code)")
                            .font(Font.custom("Roboto Mono", size: CGFloat(20)))

                    }
                }
                .padding([.top, .leading, .trailing], 20.0)
                Spacer()
                HStack() {
                    VStack() {
                        Text("YOUR TARGET IS: ")
                            .font(Font.custom("Roboto Mono", size: 25))
                            .padding(.bottom, 11.0)
                        Text("JOHN APPLESEED")
                            .font(Font.custom("Roboto Mono", size: 40))
                            .fontWeight(.black)
                            .foregroundColor(Color.gray)
                            .multilineTextAlignment(.center)
                        Image("headshot")
                            .cornerRadius(10.0)
                    }
                }//hs
                Spacer()

                HStack() {
                    VStack(alignment: .center) {
                        Text("GRADE: ")
                            .font(Font.custom("Roboto Mono", size: 15))
                            .multilineTextAlignment(.center)
                        Text("FACULTY")
                            .font(Font.custom("Roboto Mono", size: 20))

                    }
                    VStack(alignment: .center) {
                        Text("DORM: ")
                            .font(Font.custom("Roboto Mono", size: 15))
                            .multilineTextAlignment(.center)
                        Text("DAY")
                            .font(Font.custom("Roboto Mono", size: CGFloat(20)!))

                    }
                } //hs
                Spacer()

            }

            .frame(minWidth: CGFloat(0)!, maxWidth: .infinity, minHeight: CGFloat(0)!, maxHeight: .infinity, alignment: Alignment.topLeading)
        } // zstack
    } // body view
} // struct

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

这是eliminationModel文件:


import Foundation

struct EliminationModel {

func printTest() {
    print("hello world")
}

}

1 个答案:

答案 0 :(得分:3)

此行不正确:

EliminationModel.printTest()

printTest是一个实例方法:

EliminationModel().printTest()

CGFloat(0)!不是合法的,因为CGFloat(0)不是可选的。 (此代码中也不需要CGFloat()。)

.frame(minWidth: 0)!, maxWidth: .infinity, minHeight: CGFloat(0)!, maxHeight: .infinity, alignment: Alignment.topLeading)

此行应为:

.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .topLeading)

(尽管我不确定您是否需要在此处将minWidthminHeight设置为0。)


此修饰符未附加任何内容;这似乎是一个错字:

.textFieldStyle(RoundedBorderTextFieldStyle())

还有另一个错误的CGFloat(Int)!

.font(Font.custom("Roboto Mono", size: CGFloat(20)!))

这应该是:

.font(Font.custom("Roboto Mono", size: 20))

我调试此问题的方法是重复使用Cmd-Shift-A>“提取子视图”以缩小视图。对于编译器来说,拥有一个庞大的View很难进行类型检查,并且它倾向于将您指向错误的行。

将所有内容放回原处(但是我建议将其重构为较小的视图。)

struct ContentView: View {

    var eliminationModel = EliminationModel()
    @State private var code: String = "0000"
    let roboto = UIFont(name: "Roboto Mono", size: UIFont.systemFontSize)


    var body: some View {
        ZStack() {
            Image("background")
                .resizable()
                .edgesIgnoringSafeArea(/*@START_MENU_TOKEN@*/.all/*@END_MENU_TOKEN@*/)
            VStack() {
                HStack() {
                    VStack() {
                        Text("CODE:")
                            .font(Font.custom("Roboto Mono", size: 15))
                            .multilineTextAlignment(.center)
                        Text("6969")
                            .font(Font.custom("Roboto Mono", size: 10))
                            .padding(.top, 10.0)

                        Button(action: {
                            EliminationModel().printTest()
                        }) {
                            Text("Button")
                        }
                    }

                    .padding(.horizontal, 50)

                    VStack() {

                        // FIXME: Unclear what this is supposed to be part of
//                        .textFieldStyle(RoundedBorderTextFieldStyle())
                        Text("INPUT: \(code)")
                            .font(Font.custom("Roboto Mono", size: 20))

                    }
                }
                .padding([.top, .leading, .trailing], 20.0)
                Spacer()
                HStack() {
                    VStack() {
                        Text("YOUR TARGET IS: ")
                            .font(Font.custom("Roboto Mono", size: 25))
                            .padding(.bottom, 11.0)
                        Text("JOHN APPLESEED")
                            .font(Font.custom("Roboto Mono", size: 40))
                            .fontWeight(.black)
                            .foregroundColor(Color.gray)
                            .multilineTextAlignment(.center)
                        Image("headshot")
                            .cornerRadius(10.0)
                    }
                }//hs
                Spacer()

                HStack() {
                    VStack(alignment: .center) {
                        Text("GRADE: ")
                            .font(Font.custom("Roboto Mono", size: 15))
                            .multilineTextAlignment(.center)
                        Text("FACULTY")
                            .font(Font.custom("Roboto Mono", size: 20))
                    }
                    VStack(alignment: .center) {
                        Text("DORM: ")
                            .font(Font.custom("Roboto Mono", size: 15))
                            .multilineTextAlignment(.center)
                        Text("DAY")
                            .font(Font.custom("Roboto Mono", size: 20))

                    }
                } //hs
                Spacer()

            }

            .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .topLeading)
        } // zstack
    } // body view
} // struct