在 Swift 浮点值和 "X" 中创建类型:Polloquin 练习符号
Making Types in Swift Floating Point Value and "X": Polloquin Exercise Notation
我正在尝试为举重编写时间代码。重复有四个阶段:
古怪(降低重量所花费的时间)
底部(在电梯底部停留的时间)
同心(举重所花费的时间)
顶部(在电梯顶部花费的时间)
格式如下:1030
所以在那个例子中,一个人会用 1 秒的时间放下重物,然后立即用 3 秒的时间举起重物,到达动作的终点并停下来完成一次重复。
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" 来指示 "as fast as possible." 我在想我需要为此创建一个新类型吗?所以我希望能够接受一个浮点数或那个特定的字符......完全不知道如何去做。
感谢您的回复
好的,这是一种解决方法。
首先为您的数据创建一个模型:
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.
}
我是如何最终使用下面的解决方案的。仍然觉得应该有一种方法可以使用 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
我正在尝试为举重编写时间代码。重复有四个阶段:
古怪(降低重量所花费的时间) 底部(在电梯底部停留的时间) 同心(举重所花费的时间) 顶部(在电梯顶部花费的时间)
格式如下:1030
所以在那个例子中,一个人会用 1 秒的时间放下重物,然后立即用 3 秒的时间举起重物,到达动作的终点并停下来完成一次重复。
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" 来指示 "as fast as possible." 我在想我需要为此创建一个新类型吗?所以我希望能够接受一个浮点数或那个特定的字符......完全不知道如何去做。
感谢您的回复
好的,这是一种解决方法。
首先为您的数据创建一个模型:
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.
}
我是如何最终使用下面的解决方案的。仍然觉得应该有一种方法可以使用 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