0

I am trying to code times for weightlifting. There are four phases to a repetition:

Eccentric (Time spent lowering the weight) Bottom (Time spent at the bottom of the lift) Concentric (Time spent lifting the weight) Top (Time spent a the top of the lift)

It will be formatted like this: 1030

so in that example, a person would take 1 second lowering the weight, then immediately lift the weight taking three seconds, reach the end of the movement and stop to complete one repetition.

class rep {

    var eccentric:Float //  time spent lowering the weight
    var bottom:Float // time spent at the bottom of the repetition.
    var concentric:Float  // time spent raising the weight.
    var top:Float // time spent at the top of the repetition.

    var notation:String

    init(timeDown:Float, timeBottom:Float, timeUp:Float, timeTop:Float)        {

    eccentric = timeDown
    bottom = timeBottom
    concentric = timeUp
    top = timeTop

    notation = "\(eccentric),\(bottom),\(concentric),\(top)"

}

func displayNotation() -> String{

    print(notation)

    return notation

    }

}





class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()


   let repetition = rep(timeDown: 1,timeBottom: 0,timeUp: 3,timeTop: 0)

  repetition.displayNotation()



}

this outputs 1.0,0.0,3.0,0.0

What I want to do is have an additional character "X" to indicate "as fast as possible." I am thinking that I would need to create a new type for this? So I want to be able to accept a float or that particular character... totally baffled as to how to go about this.

Thanks for any response

  • What exactly do you mean by "an additional character X"? As in where would this character be used and in what circumstances? – xoudini May 29 '16 at 19:13
  • user input - the user will be able to select, for each of the four phases of the lift, a float value from 0.0 to 9.9 or an "X". This would be done with a picker view element that I would need to populate with values from 0.0 to 9.9 and an x. Also displaying the data elsewhere in the app and doing calculations with the inputted data. I could just use strings however this would mean converting the user input from a String to a Float if they did not select "X" and I thought there may be a less clunky way to do this – user1923938 May 30 '16 at 08:17
  • Alright, see if my answer below satisfies your needs. – xoudini May 30 '16 at 10:24

2 Answers2

0

Ok, so this is one way to go about it.

First create a model for your data:

class Data {
    var string: String
    var value: Double?

    init(string: String, value: Double?) {
        self.string = string
        self.value = value
    }
}

The string will be used for displaying, and the value will be used for calculations. I set the value as an optional which will be explained in a moment.


Then create a data source for the UIPickerView and populate it:

var dataSource: [Data] = []

// Adds all values from 0.0 to 9.9 and the "additional character".
func populateDataSource() {
    for i in 0..<100 {
        let value = Double(i) / 10
        dataSource.append(Data(string: value.description, value: value))
    }
    dataSource.append(Data(string: "X", value: nil))
}

What I've done here is set the value for the additional character to nil.


Assuming you've already configured your UIPickerView, add the UIPickerViewDataSource methods:

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    return 1
}

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return dataSource.count
}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return dataSource[row].string
}

// This variable will be used to hold the user selection.
var selected: Data?

// If you want it to default to e.g. 0.0, just create it as:
// var selected = dataSource.first

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    self.selected = dataSource[row]
}

And now you can do your calculations based on the selection:

// You should check that selection isn't nil before doing this.
// Depends on how you create it.

if let value = selection.value {
    // The selection has a value between 0.0 and 9.9.
    // So, do your standard calculations for that.
} else {
    // The selection does not have a value, which means it's your custom character.
    // So, take that into account in your calculations.
}
xoudini
  • 7,001
  • 5
  • 23
  • 37
  • wow thank you for that. Much more in depth than I had hoped for. I had thought about just using a class however I really thought I'd be able to use an enum somehow - guess not. I've decided on ints instead of floats now but this is how it will be used more or less: – user1923938 May 31 '16 at 18:37
0

How I've ended up using the solution below. Still feel like there should be a way to do this with CharacterSets or Enums but it's time to move on.

import UIKit

class Data {

    var string: String
    var value: Int?

    init(string:String, value:Int?){

        self.string = string
        self.value = value

    }

}


var dataSource: [Data] = []

func populateDataSource(){

    for i in 0..<10{

        dataSource.append(  Data(string:i.description, value:i)  )

    }

    dataSource.append(  Data(string:"X", value: nil)    )

}

class Notation {

    var concentric: Data
    var bottom: Data
    var eccentric: Data
    var top: Data

    var display: String

    init(down:Data,bottom:Data,up:Data,top:Data){

        self.concentric = down
        self.bottom = bottom
        self.eccentric = up
        self.top = top

        display = "\(self.concentric.string)/\(self.bottom.string)/\(self.eccentric.string)/\(self.top.string)"

    }
}


class Exercise{
    var sets, reps, rest, weight:Int
    var tempo:Notation

    init(sets:Int,reps:Int,rest:Int,weight:Int?,tempo:Notation){

        self.sets = sets
        self.reps = reps
        self.rest = rest
        self.weight = weight!
        self.tempo = tempo

    }

}

populateDataSource()


var maxStrengthTempo = Notation(
    down: dataSource[3],    // 1
    bottom: dataSource[1],  // 1
    up: dataSource[10],     // X
    top: dataSource[0]      // 0
)

var deadlift = Exercise(sets: 3, reps: 5, rest: 60, weight:200, tempo: maxStrengthTempo)


print(deadlift.tempo.display)

// console:   3/1/X/0