'Incorrect argument label' 当 Swift 中的方法重载时

'Incorrect argument label' when method overloading in Swift

我正在尝试使用以下代码在 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[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 行收到一个错误:

Incorrect argument labels in call (have _:playerIndex:, expected _: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)
    }
}

另见 .sort in protocol extension is not working