Posts

Showing posts from July, 2020

SwiftUI Data Binding with the help of @State and @Binding

Image
Data Binding is an approach which binds the data source (i.e provider) to a consumer and vice-versa , so as to keep them synchronized. UI Data binding is something which we will be interested to learn along with SwiftUI. UI Data binding is a design pattern to simplify the development of GUI applications. The name itself gives us an idea about the data will be binding to the View and vice-versa. That is , when data updates the view updates or when the view updates so does the data gets updated. Just to take as an example, there are many javascript framework which support them example ReactNative or Angular JS Framework. What would be the traditional approach? UITextField changes will be identified using the UITextFileDelegate or add Target to get notified when UIControlEventEditingChanged. Once we receive the notification we update the object. Seems like it will be too much of code to handle such behavior. Lately with iOS 13 SwiftUI , we are now able to take advantage of this design p

Understanding Property Wrappers Swift

Property Wrappers Property wrappers in swift help us to separate the code that manages the validation and the code that actually defines it. Code that defines the property var   userId :  Int Code that manages the storage @ UserIdentifier var   userId : Int @ UserIdentifier  is a custom way to manage the storage. Don't worry it will be more clear in some time, but my whole point was to clear the understanding on the difference between defining and managing a property (with the help of Property Wrapper).  Consider a use case of UserIdentifier: We want to have a userId which can take maximum up to 100 ids. If a user tries to set it to a value more than 100, it won't allow Provide a validation flag if value exceeds then 100. i.e true if any adjustments made or else false.  Think of this validation to be added at 5 different properties of different class . It would be very difficult to manage , may be write extension to handle this.  extension Int {     func validateMax () -&g