The best way to use Swift is to take advantage of the fast property observer. Suppose you have a user profile profile:
- First enter the last and middle name to enter
- Image to select and display user image
- Date field to select a date from the picker view
Your model will look like this:
struct ProfileModel { var isUpdated: Bool = false var firstName: String { willSet { if newValue != firstName { isRecentUpdated = true } } } var middleName: String { willSet { if newValue != middleName { isRecentUpdated = true } } } var lastName: String { willSet { if newValue != lastName { isRecentUpdated = true } } } var profilePhoto: URL { willSet { if newValue != profilePhoto { isRecentUpdated = true } } } var dob: Date { willSet { if newValue != dob { isRecentUpdated = true } } } }
Here, the model has the isUpdated state variable , which monitors the change in the state of the model whenever a property is changed to a new value - we checked whether the user’s action really changed the existing value in the model.
When you launch the profile screen, you can set isUpdated to false so that you can keep track of updated profile data each time you launch the profile view.
OR
If you populate this model from the server, then after each selection, you can reset the state of the model (isUpdated flag), which then can track any further changes.
Hope this helps.
source share