如何从 swift 中的结构过滤
How to filter from struct in swift
响应数据Json格式
[
{
"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"
},
]
模型 Class SearchPortfolioModel
import Foundation
typealias searchPortfolioModel = [SearchPortfolioModel]
struct SearchPortfolioModel: Codable {
let stock, code: String
enum CodingKeys: String, CodingKey {
case stock = "STOCK"
case code = "CODE"
}
}
投资组合视图模型
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)
}
内部 ViewController Class 添加了 UITableView 和 UISearchBar。
如何根据提供的响应从 UISearchBar 中搜索数据。
ViewController 整个解析数据显示到 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 委托方法。
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
filtered = searchPortfolioStocks.filter{ [=15=].stock.range(of: searchText, options: .caseInsensitive) != nil }
if(filtered.count == 0){
searchActive = false;
} else {
searchActive = true;
}
self.tbl_PortfolioSearch.reloadData()
}
我要按股票代号搜索!
如果我没理解错的话,你需要localizedCaseInsensitiveContains(_:)方法来过滤股票。
filtered = searchPortfolioStocks.filter {
[=10=].stock.localizedCaseInsensitiveContains(searchText) || [=10=].code.localizedCaseInsensitiveContains(searchText)
}
响应数据Json格式
[
{
"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"
},
]
模型 Class SearchPortfolioModel
import Foundation
typealias searchPortfolioModel = [SearchPortfolioModel]
struct SearchPortfolioModel: Codable {
let stock, code: String
enum CodingKeys: String, CodingKey {
case stock = "STOCK"
case code = "CODE"
}
}
投资组合视图模型
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)
}
内部 ViewController Class 添加了 UITableView 和 UISearchBar。 如何根据提供的响应从 UISearchBar 中搜索数据。 ViewController 整个解析数据显示到 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 委托方法。
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
filtered = searchPortfolioStocks.filter{ [=15=].stock.range(of: searchText, options: .caseInsensitive) != nil }
if(filtered.count == 0){
searchActive = false;
} else {
searchActive = true;
}
self.tbl_PortfolioSearch.reloadData()
}
我要按股票代号搜索!
如果我没理解错的话,你需要localizedCaseInsensitiveContains(_:)方法来过滤股票。
filtered = searchPortfolioStocks.filter {
[=10=].stock.localizedCaseInsensitiveContains(searchText) || [=10=].code.localizedCaseInsensitiveContains(searchText)
}