struct mutating func在同一结构的另一个实例中调用func

时间:2018-02-09 03:34:52

标签: swift struct swift4 local-variables mutating-function

我今天在swift中学习结构和课程,并决定尝试使用我新发现的知识来简化我之前构建的战斗计算器。现在我明白在func中调用的属性本质上是局部变量,甚至更多'let'常量。所以我知道为什么下面的代码有错误,但我无法弄清楚如何在不使用nil合并运算符(??)为代码添加更多复杂性的情况下实现我的目标。

非常感谢任何建议。

import Foundation
import Glibc


struct Unit {
   enum UnitType: String {
       case sniper
       case shocktrooper
       case infantry
       case support
   }
   let name: String
   let type: UnitType 
   var hitPoints: Int
   let attackStrength: Int

   //attack another unit
   mutating func attack(target: Unit) {
       print("\(self.name) is attacking \(target.name)...")
       if self.attackStrength > target.attackStrength {
           print("\(self.name) hit \(target.name) for
\(self.attackStrength) points of damage!")
           target.hit(target, self.attackStrength) /*error: cannont use
muatating member on imutable value: 'target is a 'let' constant */
       } else {
           self.repelled(by: target.attackStrength)
       }
   }
   //take damage from defender
   mutating func repelled(by damage: Int) {
       self.hitPoints -= damage
       print("\(name) was repelled and took \(damage) points of damage!")
   }
   //take damage from attack
   mutating func hit(for damage: Int) {
       self.hitPoints -= damage
   }
}

//declaring two seperate units
var player1 = Unit(name: "Player 1", type: .sniper, hitPoints: 10,
attackStrength: 3)
var player2 = Unit(name: "Player 2", type: .shocktrooper, hitPoints: 15,
attackStrength: 2)


func score() {
   print("The current hitpoints are: \(player1.name): \(player1.hitPoints)
& \(player2.name): \(player2.hitPoints)")
}

player1.attack(target: player2)
score()

1 个答案:

答案 0 :(得分:0)

您只需使用struct Unit { enum Kind: String { case sniper, shocktrooper, infantry, support } let name: String let kind: Kind var hitPoints: Int let attackStrength: Int mutating func attack(target: inout Unit) { print("\(name) is attacking \(target.name)...") if attackStrength > target.attackStrength { print("\(name) hit \(target.name) for \(attackStrength) points of damage!") target.hit(for: attackStrength) } else { repelled(by: target.attackStrength) } } mutating func repelled(by damage: Int) { hitPoints -= damage print("\(name) was repelled and took \(damage) points of damage!") } mutating func hit(for damage: Int) { hitPoints -= damage } } 关键字即可改变您的播放器2:

var player1 = Unit(name: "Player 1", kind: .sniper, hitPoints: 10,  attackStrength: 3)
var player2 = Unit(name: "Player 2", kind: .shocktrooper, hitPoints: 15, attackStrength: 2)
func score() {
    print("The current hitpoints are: \(player1.name): \(player1.hitPoints) & \(player2.name): \(player2.hitPoints)")
}
player1.attack(target: &player2)
score()

游乐场测试

class Unit {
    enum Kind: String {
        case sniper, shocktrooper, infantry, support
    }
    let name: String
    let kind: Kind
    var hitPoints: Int
    let attackStrength: Int
    init(name: String, kind: Kind, hitPoints: Int,  attackStrength: Int){
        self.name = name
        self.kind = kind
        self.hitPoints = hitPoints
        self.attackStrength = attackStrength
    }
    func attack(target: Unit) {
        print("\(name) is attacking \(target.name)...")
        if attackStrength > target.attackStrength {
            print("\(name) hit \(target.name) for \(attackStrength) points of damage!")
            target.hit(for: attackStrength)
        } else {
            repelled(by: target.attackStrength)
        }
    }
    func repelled(by damage: Int) {
        hitPoints -= damage
        print("\(name) was repelled and took \(damage) points of damage!")
    }
    func hit(for damage: Int) {
        hitPoints -= damage
    }
}
let player1 = Unit(name: "Player 1", kind: .sniper, hitPoints: 10,  attackStrength: 3)
let player2 = Unit(name: "Player 2", kind: .shocktrooper, hitPoints: 15, attackStrength: 2)
func score() {
    print("The current hitpoints are: \(player1.name): \(player1.hitPoints) & \(player2.name): \(player2.hitPoints)")
}
player1.attack(target: player2)
score()

这将打印

  

玩家1正在攻击玩家2 ......

     

玩家1击中玩家2获得3点伤害!

     

目前的生命值是:玩家1:10&球员2:12

您拥有的另一个选择是使用类而不是结构。您需要为您的类提供自定义初始值设定项,并从方法中删除mutating关键字

from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.keys import Keys

browser = webdriver.Chrome()
browser.get('http://droughtmonitor.unl.edu/Data/DataDownload/ComprehensiveStatistics.aspx')

browser.maximize_window()
dropdown = browser.find_element_by_xpath("""//*
[@id="dnn_ctr1009_USDMservice_CompStats_2017_aoiType_chosen"]""")
dropdown.click()
dropdown.send_keys('county')
dropdown.submit()
print("I'm done")