【问题标题】:Argument passed to call that takes no arguments when using init() method使用 init() 方法时传递给不带参数的调用的参数
【发布时间】:2021-05-26 18:38:26
【问题描述】:

我正在尝试为选择器分配默认值。最初,我找到了与字符串(“Tesco”)匹配的索引,但是当它起作用时,我将用传递给ProductDetail 结构的product.product_name 替换字符串。如果我删除 init() 方法(并取消注释相关变量),则错误消失并正常工作。我曾尝试将 var product = Product_ 声明移动到 init() 中,但这会导致不同的错误。我觉得我只是在尝试每一种组合,却不知道它们为什么不起作用。我确信它会在某个时候起作用,但我不知道为什么。

我收到了Argument passed to call that takes no argumentsProductDetail_Previews 结构中出现错误。

struct ProductDetail: View {
    // Define product as a constant that takes on the values for one specific product
    var product: Product_
    
    // Define a constant for the available brands
    //let all_brands = ["Asda", "Tesco", "Sainsbury's", "M&S"]
    
    
    @State var all_brands: [String]
    //State var currentBrand = 2
    @State var currentBrand: Int

    init(){
        self.all_brands = ["Asda", "Tesco", "Sainsbury's", "M&S"]
        
        // Find the first index in the list of all_brands where the string is present
        self.currentBrand = all_brands.firstIndex(of: "Tesco") ?? 0

    }
        
    var body: some View {
        
        
        //This is the main stack that contains everything
        VStack(alignment: .leading){
                        
        //Create a form to hold the Picker(s)
        Form{
        Text(product.product_name)
            .fontWeight(.bold)
            .font(.title)
            .padding()
            
        HStack{
        Text("EAN Code:")
        Text(product.code).padding()
              }
            
        // Picker for Brand
        Picker(selection: $currentBrand, label: Text("Brand")){
            ForEach(0 ..< all_brands.count){
                Text(self.all_brands[$0])
            }}
                            }
               }
}
        }//Closing bracket for main VStack
    


struct ProductDetail_Previews: PreviewProvider {

    static var previews: some View {
        ProductDetail(product: products[2])
    }
}

谢谢。

【问题讨论】:

  • 为什么 all_brands 是 @State 属性,它看起来从未改变过?

标签: swift swiftui


【解决方案1】:

您的初始化程序错误,它应该采用Product 输入参数。

根据输入参数初始化State 时,您需要通过使用_ 前缀访问它并分配State(initialValue:) 来分配State 属性。

struct ProductDetail: View {
    private let product: Product
    
    
    @State private var allBrands: [String]
    //State var currentBrand = 2
    @State private var currentBrand: Int

    init(product: Product) {
        self.product = product
        let brands = ["Asda", "Tesco", "Sainsbury's", "M&S"]
        self._allBrands = State(initialValue: brands)

        // Find the first index in the list of all_brands where the string is present
        self._currentBrand = State(initialValue: brands.firstIndex(of: "Tesco") ?? 0)
}

与您的问题无关,但所有@States 都应声明为private,因为它们不应该从视图之外发生变异。此外,您应该遵守 Swift 命名约定,变量名称为 lowerCamelCase,类型名称为 UpperCamelCase。

【讨论】:

    【解决方案2】:

    有不一致的地方。 init 方法不接受任何参数(这就是错误所说的),但 product 没有被初始化。

    由于@State 变量的初始值无论如何都是常量,并且您不能以这种方式初始化@State 属性我建议省略init 方法并按字面意思分配值。 p>

    struct ProductDetail: View {
        // Define product as a constant that takes on the values for one specific product
        var product: Product_
        
        @State private var all_brands = ["Asda", "Tesco", "Sainsbury's", "M&S"]
        @State private var currentBrand = 1
    
        var body: some View { ...
    

    请停止使用那些下划线字符和snake_case 名称。那是相当unswifty

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-05
      • 1970-01-01
      相关资源
      最近更新 更多