Skip to content

Instantly share code, notes, and snippets.

@krzyzanowskim
Last active December 5, 2021 10:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krzyzanowskim/cf51b16cc0ec02bb2f012a6de76b66e7 to your computer and use it in GitHub Desktop.
Save krzyzanowskim/cf51b16cc0ec02bb2f012a6de76b66e7 to your computer and use it in GitHub Desktop.
FB9762603: TextField with NumberFormatter() is broken on macOS 12 - no values
/*
16 November 2021
SwiftUI.TextField used with NumberFormatter() as a formatter does not set/update value on macOS 12 (Monterey). This is regression as it works ok on macOS 11 (BigSur)
Please find attached Playground code and code screenshot with marked bug
Related FB9180913. Reported June 15.
*/
import SwiftUI
import PlaygroundSupport
struct SampleNumberFormatter: View {
@State var message: String = "123"
@State var number: Int = 123
@State var number2: CGFloat = 123
var body: some View {
HStack {
// macOS 11 API
// OK in macOS 11, Broken on macOS 12
TextField("000",
value: $number2, // Here value does not update on edit
formatter: NumberFormatter()
)
// macOS 12 API
// OK
TextField("000",
value: $number,
format: .number
)
}
.frame(width: 100)
}
}
struct SampleTextFormatter: View {
@State var message: String = "123"
var body: some View {
HStack {
TextField("000",
value: $message, // Properly set initial value
formatter: TextFormatter()
)
}
.frame(width: 100)
}
// Custom dummy formatter
class TextFormatter: Formatter {
override func string(for obj: Any?) -> String? {
obj as? String
}
override func getObjectValue(_ obj: AutoreleasingUnsafeMutablePointer<AnyObject?>?, for string: String, errorDescription error: AutoreleasingUnsafeMutablePointer<NSString?>?) -> Bool {
obj?.pointee = string as AnyObject
return true
}
}
}
PlaygroundPage.current.setLiveView(SampleNumberFormatter())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment