Created
May 23, 2020 04:59
-
-
Save zacharyvoase/5ef28ce50e274c91143daebaa6d43722 to your computer and use it in GitHub Desktop.
binding.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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