gpt4 book ai didi

swift - 无法选择 pickerView 的默认值

转载 作者:行者123 更新时间:2023-11-28 12:19:18 24 4
gpt4 key购买 nike

我有一个 pickerView,其中有很多主题。每个主题都是这样的类:

class subjects {
var idSubject: String = "";
var nameSubject: String = "";
var notesSubject: String = "";
var colorSubject: String = "";

init(subjectId: String, subjectName: String, subjectNotes: String, subjectColor: String) {
idSubject = subjectId
nameSubject = subjectName
notesSubject = subjectNotes
colorSubject = subjectColor
}

func printSubject(){
print(idSubject," - ",nameSubject," - ",notesSubject," - ",colorSubject)
}
}

我这样设置我的 pickerView:

public func numberOfComponents(in pickerView: UIPickerView) -> Int{
return 1
}

public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
return MenuViewController.subjectsArray.count
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
self.view.endEditing(true)
return MenuViewController.subjectsArray[row].nameSubject
}

我想选择特定主题的行,但我不能,因为使用 indexOf 它“无法将类型 'String' 的值转换为预期的参数类型 '(subjects) throws -> Bool'"在这行代码中:

if let index = MenuViewController.subjectsArray.indexOf("Matematica") {
self.subjectsMenu.selectRow(index, inComponent: 0, animated: true)
}

有人可以帮助我吗?

最佳答案

if let index = MenuViewController.subjectsArray.index(where: {
$0.nameSubject == "Matematica"
}) {
self.subjectsMenu.selectRow(index, inComponent: 0, animated: true)
}

关于swift - 无法选择 pickerView 的默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45400355/

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