I want to implement a search bar in my app, However I can't access the methods to manipulate strings such filter, lowercase, contain etc. Can anyone provide a suggestion on how can I solve this issue ? I am a beginner.
Here's my model
class Articles: NSObject{
var author: String?
var title: String?
var publishedAt: String?
var urlImage: String?
var urlWebsite : String?
}
class LatestNewsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
func parseData(data:Data)-> [Articles] {
var articles: [Articles]? = [] // holds parsed data
Here's my model
class Articles: NSObject{
var author: String?
var title: String?
var publishedAt: String?
var urlImage: String?
var urlWebsite : String?
}
class LatestNewsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
Code Block let newsData = Articles() //Model object let urlRequest = "https://newsapi.org/v2/everything?q=coronavirus&apiKey=" //Website API var urlSelected = "" var articles: [Articles]? = [] // holds array of Articles data var indexOfPageToRequest = 1 @IBOutlet weak var table_view: UITableView!
func parseData(data:Data)-> [Articles] {
var articles: [Articles]? = [] // holds parsed data
Code Block do { let jsonResult = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary let jsonArticles = jsonResult?["articles"] as? [AnyObject] ?? [] // gets first head of json file and converts it to dictionary for jsonArticle in jsonArticles{ // captures data and stores it in the model object let article = Articles() article.author = jsonArticle["author"] as? String article.title = jsonArticle["description"] as? String article.publishedAt = jsonArticle["publishedAt"] as? String article.urlImage = jsonArticle["urlToImage"] as? String article.urlWebsite = jsonArticle["url"] as? String articles?.append(article) //put article data in the array } print(jsonArticles) DispatchQueue.main.async { if(articles!.count > 0) { self.table_view.reloadData() } } } catch { print("Nothing my guy\(error)") } return articles ?? [] // returns an array of articles } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return articles?.count ?? 0 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! NewsTableViewCell cell.authorName.text = articles?[indexPath.row].author cell.headLine.text = articles?[indexPath.row].title cell.newsImage.downloadImage(from:(self.articles?[indexPath.item].urlImage ?? "nill")) cell.timePublication.text = articles?[indexPath.row].publishedAt return cell } func searchBar(_ searchBar: UISearchBar,textDidChange searchText: String){ filteredData = [] }