1

Response Data Json format

[
 {
   "STOCK": "20 Microns Ltd. EOD Prices",
   "CODE": "BOM533022"
 },
 {
   "STOCK": "3i Infotech Ltd. EOD Prices",
   "CODE": "BOM532628"
 },
 {
   "STOCK": "3m India Ltd. EOD Prices",
   "CODE": "BOM523395"
 },
 {
   "STOCK": "7seas Technologies Ltd-$ EOD Prices",
   "CODE": "BOM590116"
 },
]

Model Class SearchPortfolioModel

import Foundation

typealias searchPortfolioModel = [SearchPortfolioModel]

struct SearchPortfolioModel: Codable {
    let stock, code: String    

    enum CodingKeys: String, CodingKey {
        case stock = "STOCK"
        case code = "CODE"
    }
}

PortfolioViewModel

func stockCodeValue(completion: @escaping () -> ()) {
    portfolioClient.fetchSearchPortfolio(){ searchPortfolioModel in
        self.searchPortfolioModel = searchPortfolioModel
        completion()
    }
}

struct PortfolioSearch{

    var stockName: String
    var stockCode: String
}

func portfolioSearchForItemAtIndexPath(indexPath: NSIndexPath) -> (SearchPortfolioModel){

    let stockName = searchPortfolioModel?[indexPath.row].stock ?? ""
    let stockCode = searchPortfolioModel?[indexPath.row].code ?? ""

    return SearchPortfolioModel(stock: stockName, code:stockCode)
}

Inside ViewController Class Added UITableView & UISearchBar. How to search the data from UISearchBar based on the provided response. In ViewController whole parse data displayed into tableview

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{

        if(searchActive) {
            return filtered.count
        }
//        return data.count;
        return portfolioViewModel.numberOfItemsInPortfolioSearchSection(section: section)
    }

    func configurationNewsCell (cell: AddPortfolioTableViewCell, forRowAtIndexPath indexPath:NSIndexPath){
        let searchPortfolioStocks = portfolioViewModel.portfolioSearchForItemAtIndexPath(indexPath: indexPath as NSIndexPath)

// Filtered data for stock & code need to will update based on search string.
        if(searchActive){
         cell.lbl_PortfolioStockName?.text = filtered[indexPath.row]
         cell.lbl_PortfolioStockCodeName?.text = searchPortfolioStocks.code

        } else {
            cell.lbl_PortfolioStockName?.text = searchPortfolioStocks.stock
            cell.lbl_PortfolioStockCodeName?.text = searchPortfolioStocks.code

        }
    }

UISearchBar Delegate Method.

    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

      filtered = searchPortfolioStocks.filter{ $0.stock.range(of: searchText, options: .caseInsensitive) != nil }

        if(filtered.count == 0){
            searchActive = false;
        } else {
            searchActive = true;
        }
        self.tbl_PortfolioSearch.reloadData()
    }

I want to search it with based on stock & code name!

4
  • 1
    You don't need to use NSString to perform a case-insensitive compare. Commented Apr 17, 2019 at 18:55
  • @rmaddy I have updated my question in order to put it correct way for understand. Commented Apr 17, 2019 at 19:55
  • 1
    And what is wrong with the current code? Commented Apr 17, 2019 at 20:25
  • @JoakimDanielson Its now working! I failed to pass proper data type to filtered data []. Commented Apr 17, 2019 at 20:39

1 Answer 1

1

If I understand you correctly, you need localizedCaseInsensitiveContains(_:) method to filter stocks.

filtered = searchPortfolioStocks.filter {
    $0.stock.localizedCaseInsensitiveContains(searchText) || $0.code.localizedCaseInsensitiveContains(searchText)
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.