Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save zacharyvoase/5ef28ce50e274c91143daebaa6d43722 to your computer and use it in GitHub Desktop.
Save zacharyvoase/5ef28ce50e274c91143daebaa6d43722 to your computer and use it in GitHub Desktop.
binding.swift
func intTextBinding(
intBinding: Binding<Int?>,
numberFormatter: NumberFormatter = NumberFormatter()) -> Binding<String> {
return Binding(
get: { numberFormatter.string(for: intBinding.wrappedValue)! },
set: { numberString in
if let number = numberFormatter.number(from: numberString) {
intBinding.wrappedValue = Int(truncating: number)
} else {
intBinding.wrappedValue = nil
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment