如何设置选择器标题以使用变量 float 作为初始值? (Swift)

How do I set a picker title to use a variable float as the initial value? (Swift)

在 Swift 中,我正在尝试设置一个选择器,以便它将返回的值用作行的初始标题。我知道如何将初始标题设置为数组中的固定位置:

class DiagnosticLensVC : UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

let myFloatArray : [Float] = [40,40.25,40.5,40.75,41,41.25,41.5,41.75,42,42.25,42.5,42.75,43,43.25,43.5,43.75,44,44.25,44.5,44.75,45,45.25,45.5,45.75,46,46.25,46.5,46.75,47,47.25,47.5,47.75,48,48.25,48.5,48.75,49,49.25,49.5,49.75,50]

// picker view stuff

func viewDidLoad() {
    super.viewDidLoad()

    myPicker.selectRow(0, inComponent: 0, animated: true) 

  }
}

但是我如何将初始行设置为从函数返回的值?有没有办法使用字典数组 [Int : Float],然后将键设置为数组编号,将值设置为我需要用于计算的浮点数,然后将键设置为索引数组?有人可以在这里指出我正确的方向吗?

不确定我是否理解正确你的问题:

您有一个 return 是浮点数的函数,现在您想将 pickerView 设置为数据数组中等于该值的条目?

将浮点数与固定值进行比较通常是个坏主意,会让您抓狂。如果您的函数的结果值肯定是数组值之一,那么根据某些计算,return 数组索引而不是浮点值可能要好得多。

查找数组中元素的索引:

if let index = find(myFloatArray,42.75) {
    println(index)   // prints 11
    myPicker.selectRow(index, inComponent: 0, animated: true) 
}