斯威夫特 - 电话中的额外争论

时间:2014-07-17 04:43:11

标签: swift

我试图从DetailViewController类调用ViewController类中声明的函数。

当试图调试“调用额外参数”时会弹出错误。

在ViewController类中:

func setCity(item : Cities, index : Int)
{

    citiesArray!.removeObjectAtIndex(index)
    citiesArray!.insertObject(item, atIndex: index)
}

详细的ViewController类

 // city of type Cities
 ViewController.setCity(city ,5 ) //Error: "Extra argument in call" 

这很简单,但我很困惑。

8 个答案:

答案 0 :(得分:81)

在某些情况下,"呼叫中的额外参数"即使调用看起来正确,如果参数的类型与函数声明的类型不匹配,也会给出。从您的问题来看,您似乎正在尝试将实例方法称为类方法,我发现这是其中一种情况。例如,此代码提供完全相同的错误:

class Foo {

    func name(a:Int, b: Int) -> String {
        return ""
    }
}

class Bar : Foo {    
    init() {
        super.init()
        Foo.name(1, b: 2)
    }
}

您可以通过将setCity的声明更改为class func setCity(...)(在评论中提到)来解决此问题;这将允许ViewController.setCity调用按预期工作,但我猜测您希望setCity成为实例方法,因为它似乎可以修改实例状态。您可能希望获取ViewController类的实例并使用它来调用setCity方法。使用上面的代码示例进行说明,我们可以更改Bar:

class Bar : Foo {    
    init() {
        super.init()
        let foo = Foo()
        foo.name(1, b: 2)
    }
}

Voila,没有更多错误。

答案 1 :(得分:11)

在我的情况下,从静态函数调用非静态函数会导致此错误。将功能更改为静态修复了错误。

答案 2 :(得分:2)

你必须这样称呼它:

ViewController.setCity(city, index: 5)

Swift有(作为Objective-C)命名参数。

答案 3 :(得分:2)

如果类/结构方法与具有相同名称但不同参数的全局方法之间存在冲突,则会发生此错误。例如,以下代码将生成此错误:

enter image description here

您可能想要检查setCity方法是否存在此类冲突。

答案 4 :(得分:1)

当编译器突出显示的表达式没有任何错误并且指定的参数没有任何错误时,我遇到了这个错误,但是在完全不同的行上有一个错误以某种方式链接到原始的。例如:用对象(b)和(c)初始化对象(a),它们自己分别用(d)和(e)初始化。编译器在(b)上说了额外的参数,但实际上错误是(e)的类型和(c)的预期参数之间的类型不匹配。

所以,基本上,检查整个表达式。如有必要,将其分解为临时变量。

等待Apple修复它。

答案 5 :(得分:0)

SwiftUI:

当您的所有代码都是正确的(实例化视图时),但视图的最大数量时,也会显示此错误消息“调用中的额外参数”超出了容器。最大值= 10,因此,如果它们之间有一些不同的TextView,图像和一些Spacers(),则很快就会超过此数目。

我遇到了这个问题,并通过将某些视图“分组”到子容器“ Group”中解决了该问题:

            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") })
        }

答案 6 :(得分:0)

如果您在 SwiftUI

中有超过 10 个 ViewBuilder 参数视图,也会发生这种情况

答案 7 :(得分:-3)

在最新的Swift 2.2中,我遇到了类似的错误,这让我花了一些时间来解决这个愚蠢的错误

class Cat {
    var color: String
    var age: Int

    init (color: String, age: Int) {
        self.color = color
        self.age = age
    }

    convenience init (color: String) {
        self.init(color: color, age: 1){ //Here is where the error was "Extra argument 'age' in call
        }
    }
}


var RedCat = Cat(color: "red")
print("RedCat is \(RedCat.color) and \(RedCat.age) year(s) old!")

修复非常简单,只需删除额外的' {}'在' self.init之后(颜色:颜色,年龄:1)'做了诀窍,即

 convenience init (color: String) {
        self.init(color: color, age: 1)
  }

最终给出以下输出

"RedCat is red and 1 year(s) old!"
相关问题