在 Swift 浮点值和"X"中制作类型:波罗昆练习符号



我正在尝试为举重编码时间。 重复有四个阶段:

偏心(减轻重量所花费的时间)底部(在电梯底部花费的时间)同心(举重所花费的时间)顶部(在电梯顶部花费的时间)

它的格式如下:1030

所以在这个例子中,一个人会花1秒钟来降低重量,然后立即举起重量,花三秒钟,到达运动的终点并停下来完成一次重复。

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()

}

输出 1.0,0.0,3.0,0.0

我想做的是增加一个字符"X"来表示"尽可能快"。 我在想我需要为此创建一个新类型? 所以我希望能够接受一个浮标或那个特定的角色......完全不知道该怎么做。

感谢您的任何回复

好的,所以这是一种方法。

首先为数据创建一个模型:

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

string将用于显示,value将用于计算。我将value设置为可选,稍后将解释。


然后为UIPickerView创建数据源并填充它:

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))
}

我在这里所做的是将附加字符的value设置为 nil .


假设您已经配置了UIPickerView,请添加UIPickerViewDataSource方法:

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]
}

现在,您可以根据选择进行计算:

// 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.
}
<</div> div class="one_answers">

我最终如何使用下面的解决方案。 仍然觉得应该有一种方法可以使用CharacterSets或Enums来做到这一点,但现在是时候继续前进了。

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

相关内容

  • 没有找到相关文章

最新更新