#7 換算 App,匯率換算

題目:換算 App,比方小費,BMI,BMR,匯率,單位換算

目的:學習字串轉數字,optional binding,收鍵盤(功能待補)。

因為種種原因,搞得自己像是期中考前一晚在趕作業
收鍵盤的功能等後續再增補惹

找到 佛心來的API 計算即時匯率

因為使用佛心API,所以就使用該組織定義的幣別代號當作 picker view 選項

畫面規劃三部分

上半部為持有幣別的設定

*國家:使用 picker view,國名透過 struct 建立對應表並顯示之

*幣別:當選擇國家後會自動填入對應的幣別(與 API 對應)

*金額:要兌換的金額

*另外提供六個常用國家的按鈕,方便快速切換

中間為計算按鈕

當轉換金額有異動時而其他條件不變情況下,可以逕行點選該按鈕重新計算計算

下半部為欲兌換的國家設定

*國家:使用 picker view,國名透過struct建立對應表並顯示之

*轉換結果:顯示小數兩位

*另外提供六個常用國家的按鈕,方便快速切換

處理過程

1. 構思題目

想了太多天時間終於選定匯率計算…然後時間就不夠用了 @@
想要即時匯率,又不想花錢去申請,在谷歌大神找到可用的 API,抓到匯率的 json 資料後寫到 dict

//https://open.er-api.com/v6/latest/TWD
//PS. TWD更換成需要的幣別即可

var pickData: Array<String> = []
var pickDict: [String:Double] = [:]

func GetRate(_ code: String) {
URLSession.shared.dataTask(with: URL(string: "https://open.er-api.com/v6/latest/\(code )")!) { (data, response, error) -> Void in
// Check if data was received successfully
if error == nil && data != nil {
do {
// Convert NSData to Dictionary where keys are of type String, and values are of any type
let json = try JSONSerialization.jsonObject(with: data!, options: []) as! [String:Any]
// Access specific key with value of type String
let dict = json["rates"] as! NSDictionary
for (key, value) in dict {
let myKey = key as! String
//let index = myKey
self.pickData.append(String(myKey))
self.pickDict[String(myKey)] = value as? Double
}

} catch {
// Something went wrong
}
}
}.resume()
}

2. 持有幣別國家與轉換幣別國家 透過 picker view 做處理

宣告資料來源(國名透過 struct 建立對應表)

struct country {
let CurrencyCode: String //國家代碼
let CountryTwName: String //國家名稱tw
let CountryEnName: String //國家名稱en
}

let countryInfo = [
//country( CurrencyCode: "", CountryTwName: "請選擇", CountryEnName: " plz select one " ),
country( CurrencyCode: "AED", CountryTwName: "阿拉伯聯合大公國", CountryEnName: "United Arab Emirates" ),
country( CurrencyCode: "AFN", CountryTwName: "阿富汗", CountryEnName: "Afghanistan" ),
country( CurrencyCode: "ALL", CountryTwName: "阿爾巴尼亞", CountryEnName: "Albania" ),
...
]
var selectedCountry: country = countryInfo[0]

畫面拉 picker view 並設定 IBOutlet

@IBOutlet weak var fromCountryPickerView: UIPickerView!
@IBOutlet weak var toCountryPickerView: UIPickerView!

class 名稱後面加入 UIPickerViewDelegate, UIPickerViewDataSource


import UIKit

class exchangeViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

此時需要加入兩個 func

    //指定有幾排pickerView選單,這邊設定為1代表只有一排
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}

//指定每一個component有幾個選項(row),這邊回傳10,所以會有十個選項可以選
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return countryInfo.count
}

viewDidLoad 處理
將國別皆預設為中華民國

    override func viewDidLoad() {
super.viewDidLoad()

fromCountryPickerView.delegate = self
fromCountryPickerView.dataSource = self

toCountryPickerView.delegate = self
toCountryPickerView.dataSource = self

//init
fromCountryPickerView.selectRow(141, inComponent: 0, animated: false)
fromCurrencyUILabel.text = "TWD"
toCountryPickerView.selectRow(141, inComponent: 0, animated: false)
toCountryUILabel.text = "TWD"

exAmtUITextField.text = "1"
resultAmt.text = "1.00"

GetRate("TWD")
}

3. 細節處理

即時匯率的異動時間點

a. viewDidLoad 時
b. 持有幣別國家異動時

轉換後金額的異動時間點

a. viewDidLoad 時
b. 持有幣別國家異動時
c. 持有幣別的金額異動時
d. 欲轉換國家異動時

4. 需再調整的地方…先欠著…待補

a. 收鍵盤
b. code 優化成 optional binding

GitHub

參考

--

--