Search code examples
iosobjective-ckey-value-observingreactive-cocoakvc

Detect if at least one property of object was modified


I have class with many strings and mutable arrays. All this objects represent form data. So if I modify textfield or add new photo to array I keep this data in my properties of object.

How can I create validator that will check if some of that properties were changed.

For example I have add some photos to array. Then I save it. When I reopen this screen again I see saved photos, but right now I want to add new photos.

The initial state and state after I add new photos to array will be different and I want to track it.

The simplest way I see here save current state of all objects and then compare it with new stat.

Let's say I have next variables:

NSString *name;
NSString *lastName;
NSMutableArray *photos;

I can use for example KVC to detect if some object was modified, but it works for example for array. So if array was modified, we can suppose that the data was changed. But for example if I property name had string @"Alex" and then I put here string @"Alex" again from text field, I can compare it with previous state and current state and make a conclusion that state was changed if strings will be different. Is this right way?


Solution

    1. You can try Method Swizzling, ex: http://nshipster.com/method-swizzling/. To hook whether your set method be called.

    2. You can use KVC or Delegate to notify the changing.

    3. You can use immutable object to make sure your object is not changed, then hook when it's created.