skip to Main Content

I’m building a simple dictionary app. I’ve gotten it to display words on UItableView while user is searching or not. I’m working on assigning each individual word with its own definition but I’m facing a trouble while setting the destination for each word at words array.

Here is part of my ViewController class and its contents:

import UIKit
class ViewController: UIViewController {

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

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

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    
    let url = Bundle.main.url(forResource: "zo_words", withExtension: "txt")! // file URL for file "words_alpha.txt"

    do {
        let string = try String(contentsOf: url, encoding: .utf8)
        wordArray = string.components(separatedBy: "n")
    } catch {
        print(error)
    }
 }
}
extension ViewController: UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if searching {
            return wordSearch.count
        } else {
            return wordArray.count
                }

Here is my wordSearch function:

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

Cannot assign value of type ‘String’ to type ‘ViewController’ Error is raised in below function while building

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let destination = segue.destination as? DefinitionViewController {
        destination.word = wordArray[(tableView.indexPathForSelectedRow?.row)!] 

class DefinitionViewController:

import UIKit

class DefinitionViewController: UIViewController {
    @IBOutlet weak var lbDefinition:
        UILabel!
    var word: ViewController?
    override func viewDidLoad() {
        super.viewDidLoad()
    
        lbDefinition.text = "((word?.wordSearch)!)"
}

}

2

Answers


  1. class ViewController: UIViewController {
          static var wordSearch = [string]()
     }
    
    class DefinitionViewController: UIViewController {
     @IBOutlet weak var lbDefinition:UILabel!
     override func viewDidLoad() {
       super.viewDidLoad()
       lbDefinition.text = "(ViewController.wordSearch)"
     }
    }
    
    Login or Signup to reply.
  2. I think it’s not applicable and recommended to take an instance from the ViewController but you can do it so easy by using the below:
    Let’s say you’ve word in your viewControllerthen you have to do the below:

       override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    
            guard tableView.indexPathForSelectedRow != nil  else {
                return
            }
    
            let selectedWord = wordArray[tableView.indexPathForSelectedRow!.row]
    
            let destination = segue.destination as! DefinitionViewController
    
            destination.word = selectedWord
        }
    

    DefinitionViewController:

    import UIKit
    
    class DefinitionViewController: UIViewController {
        @IBOutlet weak var lbDefinition:
            UILabel!
        var word: String?
        override func viewDidLoad() {
            super.viewDidLoad()
            guard word != nil else {return}
            lbDefinition.text = "((word?.wordSearch)!)"
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search