Невозможно определить поток 1 в Swift 5: фатальная ошибка: индекс выходит за пределы диапазона - PullRequest
0 голосов
/ 04 апреля 2020

Я создаю приложение IOS словарь. Я создаю viewTable с searchBar, чтобы искать слова и отображать результаты поиска как набранные. Например, если пользователь вводит «app», в viewTable должно отображаться «app, apple, apply» et c. Вот что у меня есть, пожалуйста, помогите, и спасибо.

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({$0.prefix(searchText.count) == searchText;})
        searching = true
        tableView.reloadData()
   }
}

Ответы [ 3 ]

0 голосов
/ 04 апреля 2020
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({$0.prefix(searchText.count) == searchText;})**
        searching = true
        tableView.reloadData()
   }

Instead use **wordArray.filter({$0.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.
0 голосов
/ 04 апреля 2020

Прежде всего вы должны обработать случай, если поле поиска пусто.

И есть более эффективный и надежный синтаксис для фильтрации массива: range(of может фильтровать без учета регистра и с самого начала строки (anchored) одновременно.

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

Чтобы исправить ошибку, вы должны получить данные из wordSearch или wordArray в зависимости от searching. Заменить numberOfRowsInSection и cellForRowAt на

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
}
0 голосов
/ 04 апреля 2020

Вы устанавливаете счетчик массива

if searching {
  return wordArray.count

и индексируете здесь 1 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]
}
...