user1923938 user1923938 - 1 year ago 100
Swift Question

Making Types in Swift Floating Point Value and "X": Polloquin Exercise Notation

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{


return notation



class ViewController: UIViewController {

override func viewDidLoad() {

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



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

dzk dzk
Answer Source

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.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download