Skip to content

Instantly share code, notes, and snippets.

@below
Created February 28, 2020 10:08
Show Gist options
  • Save below/411f2b32986facbe670f792cf7b3aca5 to your computer and use it in GitHub Desktop.
Save below/411f2b32986facbe670f792cf7b3aca5 to your computer and use it in GitHub Desktop.
Good SwiftUI/Combine?
struct ContentView: View {
@State private var input = ""
private var formatter: NumberFormatter
init () {
formatter = NumberFormatter()
formatter.numberStyle = .decimal
}
private func transform(string input: String) -> String {
let value = 3 * (formatter.number(from: input)?.doubleValue ?? 0)
return formatter.string(from: NSNumber(value: value)) ?? ""
}
var body: some View {
VStack(alignment: .leading) {
HStack {
Text("Input: ")
TextField("Type here", text: self.$input)
}
HStack {
Text("Output:")
Text(self.transform(string: self.input))
}
}.padding()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment