我试图从DetailViewController类调用ViewController类中声明的函数.
当试图调试'调用额外参数'时会弹出错误.
在ViewController类中:
func setCity(item : Cities, index : Int)
{
citiesArray!.removeObjectAtIndex(index)
citiesArray!.insertObject(item, atIndex: index)
}
Run Code Online (Sandbox Code Playgroud)
在detailViewController类中
// city of type Cities
ViewController.setCity(city ,5 ) //Error: "Extra argument in call"
Run Code Online (Sandbox Code Playgroud)
这很简单,但我很困惑.
Jes*_*lia 82
在某些情况下,即使调用看起来正确,如果参数的类型与函数声明的类型不匹配,也会给出"调用中的额外参数".从您的问题来看,您似乎正在尝试将实例方法作为类方法调用,我发现这是其中一种情况.例如,此代码提供完全相同的错误:
class Foo {
func name(a:Int, b: Int) -> String {
return ""
}
}
class Bar : Foo {
init() {
super.init()
Foo.name(1, b: 2)
}
}
Run Code Online (Sandbox Code Playgroud)
您可以通过更改setCity的声明class func setCity(...)(在注释中提到)在代码中解决此问题; 这将允许ViewController.setCity调用按预期工作,但我猜你想setCity成为一个实例方法,因为它似乎修改了实例状态.您可能希望获取ViewController类的实例并使用它来调用setCity方法.使用上面的代码示例进行说明,我们可以更改Bar:
class Bar : Foo {
init() {
super.init()
let foo = Foo()
foo.name(1, b: 2)
}
}
Run Code Online (Sandbox Code Playgroud)
瞧,没有更多的错误.
Luk*_*ker 13
斯威夫特用户界面:
当您的所有代码都正确时(实例化视图时),还会显示此错误消息“调用中的额外参数”,但超出了容器中的最大视图数。max = 10,所以如果你有一些不同的 TextViews、图像和它们之间的一些 Spacers(),你很快就会超过这个数字。
我遇到了这个问题并通过将一些视图“分组”到一个子容器“组”来解决它,解决了这个问题:
VStack {
Text("Congratulations")
.font(.largeTitle)
.fontWeight(.bold)
Spacer()
// This grouping solved the problem
Group {
Text("You mastered a maze with 6 rooms!")
Text("You found all the 3 hidden items")
}
Spacer()
Text("Your Progress")
.font(.largeTitle)
.fontWeight(.bold)
GameProgressView(gameProgress: gameProgress)
Spacer()
Text("You can now enter Level 4")
.multilineTextAlignment(.center)
Spacer()
RHButton(title: "OK", action: { print("OK pressed") })
}
Run Code Online (Sandbox Code Playgroud)