Posts

Post not yet marked as solved
0 Replies
253 Views
Hello! I am a coding novice learning SWIFT alone for one year, now writing the first app in my life ( a Chinese dictionary) by adopting some source codes available online. I am thinking about enriching dictionary content. Below is from the swift file (DictionaryContent) where I store my dictionary contents let WORDS = [     "o": "I",     "oo": "You",     "ooo": "he", ] "lemma + meaning" seems just too simple. There should be definitely more infos , such as phonetics, POS, example etc. I have thought about perhaps using Struct to store data, but dunno how to put it into practice... In addition, I think I shall also modify code on ViewController to correspond to the changes in DictionaryContent. However, I really have no clue how to change. Here is ViewController import UIKit class ViewController: UIViewController {     @IBOutlet weak var searchBar: UISearchBar!     @IBOutlet weak var tableView: UITableView!     var words: [String: String] = [:] //maybe this shall be modified?     var searchWords: [String: String] = [:] //maybe this shall be modified?     override func viewDidLoad() {         super.viewDidLoad()                  searchBar.delegate = self         tableView.delegate = self         tableView.dataSource = self         tableView.tableFooterView = UIView(frame: .zero)         self.loadWords()     }     func loadWords (){         self.words = WORDS     } } extension ViewController: UITableViewDataSource, UITableViewDelegate{     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {         return searchWords.keys.count     }     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {         let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: SuggestionTableViewCell.self), for: indexPath) as! SuggestionTableViewCell         print(cell)         let key = Array(searchWords.keys)[indexPath.row]         cell.wordLbl.text = key         cell.meaningLabel.text = searchWords[key] ?? ""         return cell     } } extension ViewController:UISearchBarDelegate{     func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {            self.searchWords = words.filter({ (key, value) -> Bool in                return key.contains(searchText)            })            tableView.reloadData()        } } Any advice would be wholeheartedly appreciated! Thanks in advance!
Posted Last updated
.
Post marked as solved
8 Replies
766 Views
Hello! Currently I am a self-learning novice of Swift and currently writing a Chinese dictionary app. A problem just occurs: I stored my dictionary content in an independent swift file. Somehow, it is impossible to load content whenever I run the build. Maybe there is something missing within my code ? Here is how I compiled the code related to loading content on ViewController: import UIKit class ViewController: UIViewController {     @IBOutlet weak var searchBar: UISearchBar!     @IBOutlet weak var tableView: UITableView!     var words: [String: String] = [:]     var searchWords: [String: String] = [:]     override func viewDidLoad() {         searchBar.delegate = self         tableView.delegate = self         tableView.dataSource = self         tableView.tableFooterView = UIView(frame: .zero)         super.viewDidLoad()         self.loadWords()     }     func loadWords (){         self.words = WORDS     } } And here is dictionary content in another swift file. It is still under development, however: import Foundation let WORDS = [     "x": "I",     "o": "You",     "xx": "he" ] Thanks in advance!
Posted Last updated
.