如何使用数组的 "if" 语句设置搜索,非常基本的应用程序 (swift)

how to set up a search with an "if" statement for arrays, very basic app (swift)

我正在开发一个基本应用程序,它有一个包含约 1400 个项目的 listArray,每个项目有 3 个属性(icaocallsign , 图片)。它有 2 个出口:一个“textField”和一个“label”。使用我的自定义键盘(按钮),我可以将 3 个字符写入我的“文本字段”。如果这 3 个字符与我的 listArray 的 icao 项匹配,那么我希望 callsign 项显示在我的“标签”中.有没有办法用“if”语句来做到这一点?我想在“标签”中看到“美国”。这是我所拥有的: my xcode and simulator screens

  @IBOutlet var ICAOtextField: UITextField!
@IBOutlet var callsignTextLabel: UILabel!

var updatedICAOtextField = ""
var listArray = [list]()

override func viewDidLoad() {
    super.viewDidLoad()
    setUpList()
}

private func setUpList() {

listArray.append(list(icao: "AAB", callsign: "A-B-G", image: "BELGIUM"))
listArray.append(list(icao: "AAC", callsign: "ARMYAIR", image: "UK"))
listArray.append(list(icao: "AAF", callsign: "AZUR", image: "FRANCE"))
listArray.append(list(icao: "AAL", callsign: "AMERICAN", image: "USA"))
}

@IBAction func abcButton(_ sender: UIButton) {
    
    updatedICAOtextField = ICAOtextField.text! + sender.currentTitle!
    ICAOtextField.text = updatedICAOtextField
    
    if updatedICAOtextField.count > 3 {
        ICAOtextField.text = ""
    }
    
    if ICAOtextField.text ==                           {
        
        callsignTextLabel.text = "            "
    }
    
}

@IBAction func abcButton(_ sender: UIButton) {

    updatedICAOtextField = ICAOtextField.text! + (sender.titleLabel?.text)!
    ICAOtextField.text = updatedICAOtextField
    
    if updatedICAOtextField.count >= 3 {
        let index = listArray.firstIndex { (list) -> Bool in
            if list.icao == updatedICAOtextField {
                return true
            }
            return false
        }
        if index != nil {
            callsignTextLabel.text = listArray[index!].callsign
        }
        ICAOtextField.text = ""
    }
}
import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

@IBOutlet var ICAOtextField: UITextField!
@IBOutlet var callsignTextLabel: UILabel!

var updatedICAOtextField = ""
var listArray = [list]()

override func viewDidLoad() {
    super.viewDidLoad()
    
    ICAOtextField.delegate = self
    ICAOtextField.addTarget(self, action: #selector(textFieldDidChange), for: UIControl.Event.editingChanged)
    
    setUpList()
}



@objc func textFieldDidChange(textField: UITextField) {
     if ICAOtextField.text!.count >= 3 {
         let index = listArray.firstIndex { (list) -> Bool in
            if "\(list.icao)" == ICAOtextField.text {
                 return true
             }
             return false
         }
         if index != nil {
            callsignTextLabel.text = listArray[index!].callsign
         }
     }
}



private func setUpList() {

listArray.append(list(icao: "AAB", callsign: "A-B-G", image: "BELGIUM"))
listArray.append(list(icao: "AAC", callsign: "ARMYAIR", image: "UK"))
listArray.append(list(icao: "AAF", callsign: "AZUR", image: "FRANCE"))
listArray.append(list(icao: "AAL", callsign: "AMERICAN", image: "USA"))
}

@IBAction func abcButton(_ sender: UIButton) {
    
    updatedICAOtextField = ICAOtextField.text! + sender.currentTitle!
    ICAOtextField.text = updatedICAOtextField
    
    if updatedICAOtextField.count > 3 {
        ICAOtextField.text = ""
    }
}

@IBAction func clearButton(_ sender: UIButton) {
    ICAOtextField.text = ""
    callsignTextLabel.text = ""
}

}

class 列表 {

let icao: String
let callsign: String
let image: String

init(icao: String, callsign: String, image: String) {
    
    self.icao = icao
    self.callsign = callsign
    self.image = image
}

}