';参数标签不正确';在Swift中重载方法时
我正在尝试使用以下代码在Swift中进行方法重载:';参数标签不正确';在Swift中重载方法时,swift,parameters,overloading,Swift,Parameters,Overloading,我正在尝试使用以下代码在Swift中进行方法重载: struct Game { private let players: [UserProfile] //set in init() private var scores: [Int] = [] mutating func setScore(_ score: Int, playerIndex: Int) { //... stuff happening ... self.scores[pl
struct Game {
private let players: [UserProfile] //set in init()
private var scores: [Int] = []
mutating func setScore(_ score: Int, playerIndex: Int) {
//... stuff happening ...
self.scores[playerIndex] = score
}
func setScore(_ score: Int, player: UserProfile) {
guard let playerIndex = self.players.index(of: player) else {
return
}
self.setScore(score, playerIndex: playerIndex)
}
}
我在self.setScore
行中遇到错误:
调用中的参数标签不正确(应为uu3;:playerIndex:,应为3;:player:)
我已经研究这段代码有一段时间了,但是我不明白为什么它不起作用。有什么提示吗?感谢@Hamish为我指明了正确的方向 事实证明,编译器消息相当误导。问题是调用
mutating
方法的每个方法都必须是mutating
本身。这就解决了问题:
struct Game {
private let players: [UserProfile] //set in init()
private var scores: [Int] = []
mutating func setScore(_ score: Int, playerIndex: Int) {
//... stuff happening ...
self.scores[playerIndex] = score
}
mutating func setScore(_ score: Int, player: UserProfile) {
guard let playerIndex = self.players.index(of: player) else {
return
}
self.setScore(score, playerIndex: playerIndex)
}
}
另请参见您试图在非变异方法中调用一个变异方法–类似的问答:您是对的!非常感谢。如果你用它来回答,我会接受。像这样的错误信息是一条红鲱鱼的情况应该是。请写下你的解决方案作为一个正确的答案,并接受它,这样这个问题才能被考虑resolved@BlackWolf请随意将您对问题的编辑转换为自我回答(因为这是问题的所在)–个人,我认为它已经足够接近于被愚弄了(尽管令人恼火的是,它不能像被愚弄一样接近,因为答案都没有被接受或被提升)。哇,这真是一个令人困惑的编译器错误。谢谢