如何解决如何在货币转换器应用 Swift 中实现搜索栏?
我正在尝试在我的货币转换器应用程序中实现一个搜索栏。但问题是我正在使用 API 解析来自 JSON 的数据,但我不知道如何将货币缩写与我的搜索栏连接起来。我确实尝试将所有货币缩写硬编码到一个数组中,但没有奏效。任何帮助将不胜感激。相关代码如下:
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate {
struct Currency: Decodable {
let base: String
let date: String
let rates: [String : Double]
}
@IBOutlet weak var rateField: UITextField!
@IBOutlet weak var conversionTableView: UITableView!
@IBOutlet weak var button: UIButton!
@IBOutlet weak var searchBar: UISearchBar!
var eur: Currency?
var baseRate = 1.0
var data = ["AED","AFN","ALL","AMD","ANG","AOA","ARS","ATS","AUD","AWG","AZN","BAM","BBD","BDT","BEF","BGN","BHD","BIF","BMD","BND","BOB","BRL","BSD","BTN","BWP","BYR","BZD","CAD","CDF","CHF","CLP","CNY","cop","COU","CRC","CUC","CUP","CVE","CZK","DEM","DJF","DKK","DOP","DZD","EEK","EGP","ERN","ESP","ETB","EUR","FIM","FJD","FKP","FRF","GBP","GEL","GHS","GIP","GMD","GNF","Grd","GTQ","GYD","HKD","HNL","HRK","HTG","HUF","IDR","IEP","ILS","INR","IQD","IRR","ISK","ITL","JMD","JOD","JPY","KES","KGS","KHR","KMF","KPW","KRW","KWD","KYD","KZT","LAK","LBP","LKR","LRD","LSL","LTL","LUF","LVL","LYD","MAD","MDL","MGA","MKD","MMK","MNT","MOP","MRO","MUR","MVR","MWK","MXN","MYR","MZN","NAD","NGN","NIO","NLG","NOK","NPR","NZD","omr","PAB","PEN","PGK","PHP","PKR","PLN","PTE","PYG","QAR","RON","RSD","RUB","RWF","SAR","SBD","SCR","SDG","SEK","SGD","SHP","SIT","sll","SOS","SRD","STD","SVC","SYP","SZL","THB","TJS","TMT","TND","TOP","TRY","TTD","TWD","TZS","UAH","UGX","USD","UYI","UYU","UZS","VEF","VND","VUV","WST","XAF","XCD","XDR","XOF","XPF","YER","ZAR","ZMK","ZWL"]
var filteredData: [String]!
override func viewDidLoad() {
super.viewDidLoad()
searchBar.delegate = self
filteredData = data
rateField.round()
conversionTableView.round()
button.round()
conversionTableView.dataSource = self
conversionTableView.delegate = self
conversionTableView.allowsSelection = false
fetchData()
}
@IBAction func onConvertButtonTapped(_ sender: UIButton) {
if let iGetString = rateField.text {
if let isDouble = Double(iGetString) {
baseRate = isDouble
fetchData()
} else {
let alertControl = UIAlertController(title: "Wrong data!",message: "Please,use digits only!",preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "dismiss",style: .destructive,handler: nil)
alertControl.addAction(cancelAction)
present(alertControl,animated: true,completion: nil)
}
}
}
func fetchData() {
let url = URL(string: "http://data.fixer.io/api/latest?=EUR")
URLSession.shared.dataTask(with: url!) { (data,response,error) in
if error == nil {
do {
self.eur = try JSONDecoder().decode(Currency.self,from: data!)
} catch {
print("Parse Error")
}
dispatchQueue.main.async {
self.conversionTableView.reloadData()
}
} else {
print("Error")
}
}.resume()
}
func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
if let currencyFetched = eur {
return currencyFetched.rates.count
}
return 0
}
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .value1,reuseIdentifier: nil)
if let currencyFetched = eur {
cell.textLabel?.text = Array(currencyFetched.rates.keys)[indexPath.row]
let selectedrate = baseRate * Array(currencyFetched.rates.values)[indexPath.row]
cell.detailTextLabel?.text = "\(String(format: "%.2f",selectedrate))"
return cell
}
return UITableViewCell()
}
func searchBar(_ searchBar: UISearchBar,textDidChange searchText: String) {
filteredData = []
if searchText == "" {
filteredData = data
} else {
for currency in data {
if currency.lowercased().contains(searchText.lowercased()) {
filteredData.append(currency)
}
}
}
self.conversionTableView.reloadData()
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。