Arrays UIPickerView选择Swift中标题==字符串的行

Arrays UIPickerView选择Swift中标题==字符串的行,arrays,swift,uipickerview,Arrays,Swift,Uipickerview,我有一个UIPickerView,我想选择一行,其中title等于String,这是我从php/mysql脚本收到的 假设这是我的数组: var myArray = ["First","Second","Third","Fourth","Sixth","Seventh"] 这是我从php脚本中得到的,也是我希望UIPickerView选择的: let myString = "Second" 所以我的问题是: 如何执行以下操作:self.myPicker.selectRow1,不完整:0,动画

我有一个UIPickerView,我想选择一行,其中title等于String,这是我从php/mysql脚本收到的

假设这是我的数组:

var myArray = ["First","Second","Third","Fourth","Sixth","Seventh"]
这是我从php脚本中得到的,也是我希望UIPickerView选择的:

let myString = "Second"
所以我的问题是:

如何执行以下操作:self.myPicker.selectRow1,不完整:0,动画:true,但我希望myPicker选择标题为Second的行

我发现了这个问题,但它在目标c中:


多谢各位

如果您不知道数组中对象的索引,可以这样尝试

let index = myArray.indexOf(myString)
self.myPicker.selectRow(index, inComponent: 0, animated: true)
let index = myArray.index(of: myString)
如果您正在使用swift 3,请查找如下索引

let index = myArray.indexOf(myString)
self.myPicker.selectRow(index, inComponent: 0, animated: true)
let index = myArray.index(of: myString)
编辑:如果不确定数组是否包含字符串,也可以检查nil

if let index = myArray.indexOf(myString) {
    self.myPicker.selectRow(index, inComponent: 0, animated: true)
}

如果您不知道数组中对象的索引,可以这样尝试

let index = myArray.indexOf(myString)
self.myPicker.selectRow(index, inComponent: 0, animated: true)
let index = myArray.index(of: myString)
如果您正在使用swift 3,请查找如下索引

let index = myArray.indexOf(myString)
self.myPicker.selectRow(index, inComponent: 0, animated: true)
let index = myArray.index(of: myString)
编辑:如果不确定数组是否包含字符串,也可以检查nil

if let index = myArray.indexOf(myString) {
    self.myPicker.selectRow(index, inComponent: 0, animated: true)
}

谢谢这正是我一直在寻找的:欢迎伴侣:快乐编码:谢谢这正是我一直在寻找的:欢迎伴侣:快乐编码:为什么-1??:D怎么了?为什么-1??:我知道怎么了。