Unable to identify Thread 1 in Swift 5: Fatal error: Index out of range

Unable to identify Thread 1 in Swift 5: Fatal error: Index out of range

我正在构建一个 IOS 词典应用程序。我正在创建一个带有 searchBar 的 viewTable 来搜索单词并在键入时显示搜索结果。例如,如果用户输入 "app",viewTable 应该显示 "app, apple, apply" 等。这是我的,请帮助,谢谢。

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var searchBar: UISearchBar!
    @IBOutlet weak var tableView: UITableView!

    var wordSearch = [String]()
    var searching = false
    var wordArray: [String] = []

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        let url = Bundle.main.url(forResource: "words_alpha", withExtension: "txt")! // file URL for file "words_alpha.txt"
               do {
                   let string = try String(contentsOf: url, encoding: .utf8)
                   wordArray = string.components(separatedBy: CharacterSet.newlines)
               } catch {
                   print(error)
               }
    }
}

extension ViewController: UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if searching {
            return wordArray.count
        } else {
            return wordArray.count
        }

    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell");cell?.textLabel?.text = wordArray[indexPath.row]
        if searching {
            cell?.textLabel?.text = wordSearch[indexPath.row] //Thread 1: Fatal error: Index out of range
        } else {
            cell?.textLabel?.text = wordSearch[indexPath.row]
        }
            return cell!
        }
    }

extension ViewController: UISearchBarDelegate {
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        wordSearch = wordArray.filter({[=10=].prefix(searchText.count) == searchText;})
        searching = true
        tableView.reloadData()
   }
}

你设置了数组的个数

if searching {
  return wordArray.count

并在此处索引另外 1 个

if searching {
   cell?.textLabel?.text = wordSearch[indexPath.row] //Thread 1: Fatal error: Index out of range

所以使用

if searching {
  return wordSearch.count
} else {
  return wordArray.count
}

连同

if searching {
     cell?.textLabel?.text = wordSearch[indexPath.row] //Thread 1: Fatal error: Index out of range
} else {
    cell?.textLabel?.text = wordArray[indexPath.row]
}
The above answer is right . 

Adding to it. The search filtering g method seems to be wrong

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        **wordSearch = wordArray.filter({[=10=].prefix(searchText.count) == searchText;})**
        searching = true
        tableView.reloadData()
   }

Instead use **wordArray.filter({[=10=].contains(searchText)})**

And on clearing the search bar . Reset searching = false and empty the wordSearch array.
Check whether you need to set the searchBar.delegate = self in viewDidLoad.

首先你必须处理搜索字段为空的情况。

并且有一种更高效可靠的语法来过滤数组:range(of可以同时过滤不区分大小写和从字符串开头(anchored)开始的。

extension ViewController: UISearchBarDelegate {
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        if searchText.isEmpty {
            searching = false
            wordSearch.removeAll()
        } else {
            wordSearch = wordArray.filter{ [=10=].range(of: searchText, options: [.caseInsensitive, .anchored]) != nil }
            searching = true
        }
        tableView.reloadData()
   }
}

要修复错误,您必须根据 searchingwordSearchwordArray 获取数据。将 numberOfRowsInSectioncellForRowAt 替换为

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return searching ? wordSearch.count : wordArray.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for indexPath)
    let word = searching ? wordSearch[indexPath.row] : wordArray[indexPath.row]
    cell.textLabel?.text = word
    return cell
}