【问题标题】:Swift: Cannot use mutating member on immutable valueSwift:不能在不可变值上使用变异成员
【发布时间】:2020-04-03 03:36:36
【问题描述】:

我只是在慢慢地学习 Swift,我决定做一个非常简单的格斗游戏。当它是一个类时,以下所有代码都工作得很好,然后我决定将它更改为一个结构,现在在函数中会降低目标 hp 的行上,它给了我以下错误:

Cannot use mutating member on immutable value

这是整个结构:

struct Player {
    let name : String
    let maxHealth : Int
    var currentHealth : Int
    let maxMana: Int
    var currentMana: Int

    init(playerName: String) {
        name = playerName
        maxHealth = 100
        currentHealth = 100
        maxMana = 100
        currentMana = 100
    }

    mutating func swingAt(target: Player) {
        if isDead(target: self) {
            print("You are dead!")
            return
        }
        let damage = Int.random(in: 0..<10)
        target.takeDamage(amount: damage) // Error:Cannot use mutating member on immutable value: 'target' is a 'let' constant
        if damage > 0 {
            print("\(self.name) does \(damage) damage to \(target.name)!")
        }
        else {
            print("\(self.name) misses \(target.name)!")
        }
        if isDead(target: target) {
            print("\(self.name) has slain \(target.name)!")
        }
    }

    mutating func swingFiveTimesAt(target: Player) {
        if isDead(target: self) {
            print("You are dead!")
            return
        }
        for _ in 1...5 {
            let damage = Int.random(in: 0..<10)
            target.takeDamage(amount: damage) // Error:Cannot use mutating member on immutable value: 'target' is a 'let' constant
            if damage > 0 {
                print("\(self.name) does \(damage) damage to \(target.name)!")
            }
            else {
                print("\(self.name) misses \(target.name)!")
            }
        }
        if isDead(target: target) {
            print("\(self.name) has slain \(target.name)!")
        }
    }

    mutating func castFireball(target: Player) {
        if isDead(target: self) {
            print("You are dead!")
            return
        }
        if self.currentMana < 50 {
            print("\(self.name) only has \(self.currentMana), but needs 50 mana to cast this spell!")
            return
        }
        let damage = Int.random(in: 1..<50)
        target.takeDamage(amount: damage) // Error:Cannot use mutating member on immutable value: 'target' is a 'let' constant
        self.currentMana -= 50
        print("\(self.name) throws a huge fireball at \(target.name), doing \(damage) damage!")
        if isDead(target: target) {
            print("\(self.name) has slain \(target.name)!")
        }
    }

    func isDead(target: Player) -> Bool {
        if target.currentHealth <= 0 {
            return true
        } else {
            return false
        }
    }

    mutating func takeDamage(amount: Int) {
        self.currentHealth -= amount
    }
}

有什么想法可以实现所需的功能吗?

【问题讨论】:

    标签: swift


    【解决方案1】:

    我在发布之前搜索了大约 45 分钟,当然在发布之后我马上就能找到答案。原来添加'inout'修复了它!

    破碎: mutating func swingAt(target: Player) {

    工作: mutating func swingAt(target: inout Player) {

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-23
      • 2021-10-12
      • 1970-01-01
      • 2019-03-23
      • 2020-08-08
      • 2020-01-28
      相关资源
      最近更新 更多