SwiftUI 学习笔记
对 Binding 的 extension
extension Binding where Value == String{
func max (_ limit: Int) -> Self {
if self.wrappedValue.count > limit {
DispatchQueue.main.async {
self.wrappedValue = String(self.wrappedValue.prefix(limit))
}
}
return self
}
}
@State var text = ""
TextField("", text:$text.max(5))
onChange 支持获取 oldValue (before iOS 17)
.onChange(of: value) { [oldValue = value], newValue in
print("oldValue: \(oldValue), newValue: \(newValue)")
}