You asked if there is a difference between the two. The answer is yes, there is a difference between them:
First
Saying "I'm as a point of view," I add an observer named self (aka) viewControllerObject if you called it in viewController.m whenever a property called "frame" changes.
Second
Says "me like a ViewController". I add selfAsAnObserver whenever a KeyPath called "view.frame" changes.
Since each observer must implement
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
In this case, you will not notice much difference, because you added the viewController as an observer in any of the methods above, but it will matter when you are dealing with different objects. But the rule is simple, each added observer must implement
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
One more thing: Itβs a good idea to create a context for observation, for example,
//In MyViewController.m //.. static int observingViewFrameContext // In ... [self addObserver:self forKeyPath:@"view.frame" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:&observingViewFrameContext]; // .. don' forget to remove an observer ! too
source share