简体   繁体   English

通话错误谜题中的额外参数

[英]Extra argument in call error mystery

I had some older Swift code that used to compile and work where I was using the .append to build out a data structure dynamically. 我有一些旧的Swift代码,这些代码曾经在.append用来动态构建数据结构的地方编译和工作。 After upgrading to a few compiler versions newer I am getting the dreaded "Extra Argument ' ' in call" error. 升级到较新的一些编译器版本后,我得到了可怕的“额外参数'通话中”错误。 I reduced the code down to this: 我将代码简化为:

struct  EHSearch {
    let EHcategory : String = ""
    let EHname : String = ""
}

var  myEHSearch = [EHSearch]()

// Call to dynamically append the results
// Extra argument: 'EHcategory' in call

myEHSearch.append(EHSearch(EHcategory: "Food", EHname: "Joes Crab Shack"))

I can't see anything so far in searching on what has changed to cause this one so seeking guidance here. 到目前为止,我在寻找导致这一变化的变化方面看不到任何东西,因此请在此处寻求指导。

Because you have let in your struct. 因为你已经let你的结构。 Define your structure like this: 像这样定义您的结构:

struct  EHSearch {
var EHcategory : String = ""
var EHname : String = ""
}

If you have constants in your struct , you can not provide them initial value while creating new structure instances.The automatically-generated memberwise initializer doesn't accept let members as parameters of the initializer of struct. 如果struct有常量,则在创建新的结构实例时不能提供它们的初始值。自动生成的逐成员初始化器不接受let成员作为struct初始化器的参数。

It depends on your intentions with the struct's properties. 这取决于您对结构属性的意图。 Do you want them to be mutable or not? 您是否希望它们可变?

If yes, then @sasquatch's answer will do. 如果是的话,@ sasquatch的答案就可以了。

If not, then you need to ensure a value is assigned to them only once. 如果没有,那么您需要确保仅将一个值分配给它们一次。 As you already do that in the struct declaration (the default values), you can't assign new values to them. 正如您在struct声明(默认值)中所做的那样,您无法为它们分配新值。 But being a struct, they don't need to have default values - moreover, struct automatically receive a memberwise initializer. 但是作为结构,它们不需要具有默认值-而且,结构会自动接收一个成员初始化器。 https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html

So here is the variant for immutable properties: 因此,这是不可变属性的变体:

struct  EHSearch {
    let EHcategory : String
    let EHname : String
}

var  myEHSearch = [EHSearch]()

// Call to dynamically append the results
// Extra argument: 'EHcategory' in call

myEHSearch.append(EHSearch(EHcategory: "Food", EHname: "Joes Crab Shack"))

The "Extra Argument" error you're seeing is because the compiler already has values for the properties so it doesn't expect any new ones. 您看到的“额外参数”错误是因为编译器已经具有该属性的值,因此它不希望有任何新值。 Here is the "middle" way - one property has a default value whilst the other doesn't - which should make it clearer: 这是“中间”方式-一个属性具有默认值,而另一个属性没有默认值-这应该使它更清晰:

struct  EHSearch {
    let EHcategory : String = ""
    let EHname : String
}

var  myEHSearch = [EHSearch]()

// Call to dynamically append the results
// Extra argument: 'EHcategory' in call

myEHSearch.append(EHSearch(EHname: "Joes Crab Shack"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM