I have a currency converter calculator application, Where data is fetched through the API. I am trying to convert inputted data through the enum and switch but unfortunately, I am not sure what is the mistake. Please Can someone help me to understand what shall I do?
You may find my GitHub Link for a project below
https://github.com/Chokaaaa/CurMe
This is my Fetch file
import SwiftUI
class FetchData: ObservableObject {
@Published var coversionData: [Currency] = []
@Published var baseCode = "USD"
init() {
fetch()
}
func fetch() {
let url = "https://open.exchangerate-api.com/v6/latest?base=\(baseCode)"
let session = URLSession(configuration: .default)
session.dataTask(with: URL(string: url)!) { data, _, _ in
guard let JSONData = data else {return}
do {
let conversion = try JSONDecoder().decode(Conversion.self, from: JSONData)
DispatchQueue.main.async {
self.coversionData = conversion.rates.compactMap({ (key,value) -> Currency? in
return Currency(currencyName: key, currencyValue: value)
})
.filter({ Currency in
Currency.currencyName == self.filteredCurrency
})
}
}
catch {
print(error)
}
}
.resume()
}
func updateData(baseCode: String) {
self.baseCode = baseCode
self.coversionData.removeAll()
fetch()
}
}
Below you may find an enum where I use a switch with a return of input from the custom number pad (like a calculator) and multiplied on the dummy value. I think I need to fix something in the switch case. Please guys someone help me!! I am struggling.
Bellow, you may find a code for a enum/switch
import SwiftUI
struct CurrencyView: View {
@StateObject var viewModel = FetchData()
enum currencyChoice {
case Kazakhstan, Rubles, Usa
func image() -> Image {
switch self {
case .Kazakhstan: return Image("kz")
case .Rubles: return Image("rub")
case .Usa: return Image("usa")
}
}
func operation(_ input: Double) -> Double {
switch self {
case .Kazakhstan: return (input * 2)
case .Rubles: return (input * 3)
case .Usa: return (input * 5)
}
}
}
var function : currencyChoice
@Binding var state : CalculationState
var body: some View {
return function.image()
.resizable()
.scaledToFill()
.frame(width: 80, height: 80)
.cornerRadius(40)
.onTapGesture {
state.currentNumber = function.operation(state.currentNumber)
}
}
}