There isn't space here to show the entire code, so I'm trying to collect the bits that count from different parts of the app. The VC I am working with shows a progress bar for each scale on each horse. When I use a lengthy If statement, it works the way it should. However, it requires a lot of extra coding. I wonder if key paths could be a way of shortening my code (in reality there are 7 possibilities instead of three). I think I can make key paths work if I can figure out how to change var currentTraining and var currentBoost. Then I can make an IBAction to change the var and thus make the progress bar show the training progress. For now it only shows basicTraining, because that is what I have manually written in the var currentTraining. I need to do this programmatically with the scalesButton. I think I managed to include everything now, please let me know if there is anything else missing
struct Horse {		
var name: String		
var basicTraining : Float		
var rhythm : Float		
var suppleness : Float}
var myHorses = [		
Horse(name: "Donnerhall", basicTraining : 0.5, rhythm : 0.2, suppleness : 0.1),		
Horse(name: "Bjork", basicTraining : 0.4, rhythm : 0.3, suppleness : 0.1)]
var horseIndex = 0
struct Skill {
var basicBoost: Float
var rhythmBoost: Float
var supplenessBoost: Float}
var scaleCode = [ basicSkills, rhythmSkills, supplenessSkills]
var scaleIndex = 0
var basicSkills = [Skill(basicBoost: 0.1, rhythmBoost: 0, supplenessBoost: 0)]
var rhythmSkills = [Skill(basicBoost: 0, rhythmBoost: 0.1, supplenessBoost: 0)]
var supplenessSkills = [Skill(basicBoost: 0, rhythmBoost: 0.1, supplenessBoost: 0.1)]
var skillIndex = 0
var currentTraining: WritableKeyPath<Horse, Float> = \.basicTraining //or \.rhythm or \.suppleness
var currentBoost: WritableKeyPath<Skill, Float> = \.basicBoost
@IBAction func scalesButton(_ sender: UIButton) {
scaleIndex = sender.tag
switch scaleIndex {
case 0:
currentTraining = \.basicTraining
case 1:
currentTraining = \.rhythm
case 2:
currentTraining = \.suppleness
}
//should choose	.basicTraining, .rhythm, or .suppleness depending on which scalesButton the user selects.	However, some skills can boost multiple stats, so I might have to do a separate if statement for each stat
@objc func trainHorse(sender: UIButton){
if myHorses[horseIndex][keyPath: currentTraining] >= 100 {
myHorses[horseIndex][keyPath: currentTraining] = 100
} else {
myHorses[horseIndex][keyPath: currentTraining] += currentScaleCode[skillIndex].currentBoost
}
//previous code without key paths:
// if myHorses[horseIndex].basicTraining >= 100 {
// myHorses[horseIndex].basicTraining = 100
// } else {
// myHorses[horseIndex].basicTraining += currentScaleCode[skillIndex].basicBoost
// }
//
// if myHorses[horseIndex].rhythm >= 100 {
// myHorses[horseIndex].rhythm = 100
// } else {
// myHorses[horseIndex].rhythm += currentScaleCode[skillIndex].rhythmBoost
// }
//
// if myHorses[horseIndex].suppleness >= 100 {
// myHorses[horseIndex].suppleness = 100
// } else {
// myHorses[horseIndex].suppleness += currentScaleCode[skillIndex].supplenessBoost
// }
currentScaleCode = scaleCode[scaleIndex]
//the following shows a progress bar with the stat selected with the scalesButton
skillProgress.progress = myHorses[horseIndex][keyPath: currentTraining]
skillOutlet.text = "\(currentScaleName): \(myHorses[horseIndex][keyPath: currentTraining])"
}
previous code:
//switch scaleIndex {
// case 0:
// skillProgress.progress = myHorses[horseIndex].basicTraining
// skillOutlet.text = "\(currentScaleName): \(myHorses[horseIndex].basicTraining)"
// case 1:
// skillProgress.progress = myHorses[horseIndex].rhythm
// skillOutlet.text = "\(currentScaleName): \(myHorses[horseIndex].rhythm)"
// case 2:
// skillProgress.progress = myHorses[horseIndex].suppleness
// skillOutlet.text = "\(currentScaleName): \(myHorses[horseIndex].suppleness)"