gpt4 book ai didi

swift - 在 Swift 浮点值和 "X": Polloquin Exercise Notation 中创建类型

转载 作者:行者123 更新时间:2023-11-30 13:23:20 26 4
gpt4 key购买 nike

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

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

其格式如下:1030

因此,在该示例中,一个人需要 1 秒的时间放下重物,然后立即花 3 秒的时间举起重物,到达 Action 终点并停下来完成一次重复。

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”来表示“尽快”。我在想我需要为此创建一个新类型吗?所以我希望能够接受一个 float 或那个特定的字符......完全困惑于如何去做。

感谢您的回复

最佳答案

好的,这是解决这个问题的一种方法。

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

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

init(string: String, value: Double?) {
self.string = string
self.value = 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))
}

我在这里所做的是将附加字符的设置为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.
}

关于swift - 在 Swift 浮点值和 "X": Polloquin Exercise Notation 中创建类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37513432/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com