Question

This may be a ridiculous question, but I have a method like this in my view controller:

[self registerProperty:self.currentUser];

and in the implementation of registerProperty: I would like to get the string "currentUser".

I'm doing this because I want to observe the property of the view controller "currentUser", not the actual user object, so I can intercept the setter.

At the moment I'm checking the Objective-C runtime for a list of all properties of the view controller and checking if the value of the property equals the currentUser object:

-(void)registerProperty:(id)property 
{
    for (NSString *propertyName in [self allPropertiesOfClass:[property class]])
        if ([property isEqual:[self valueForKey:propertyName]])
             NSLog(@"The property passed into the method is %@", propertyName);
}

The problem with this is that I may have two properties that both contain the same user object, in which case either of them would pass that test. How could I fix this?

Was it helpful?

Solution

Pass in the object whose property you want to observe and, separately, the property name as a string. That is, mirror (a subset of) the arguments of the KVO -addObserver:... method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top