KVO

  • Through printThe monitored instance objecttheisaPointer, which can be found, of the listening instance objectisaInstead of pointing to class objects, I point toNSKVONotifying_MJPerson
  • NSKVONotifying_MJPersonIs the use ofRuntimeA dynamically created class, isMJPersonA subclass of

NSKVONotifying_MJPerson pseudo code

  • through[self.person1 methodForSelector:@selector(setAge:)]Gets the address of the method implementation
  • Through the LLDB commandp (IMP)0x1069189e4Gets the name of the method

#import "MJPerson.h"

@interface NSKVONotifying_MJPerson : MJPerson

@end

#import "NSKVONotifying_MJPerson.h"

@implementation NSKVONotifying_MJPerson

- (void)setAge:(int)age
{
    _NSSetIntValueAndNotify();
}

/ / pseudo code
void _NSSetIntValueAndNotify()
{
    [self willChangeValueForKey:@"age"];
    [super setAge:age];
    [self didChangeValueForKey:@"age"];
}

- (void)didChangeValueForKey:(NSString *)key
{
    // Notify the listener that the value of the so-and-so attribute has changed
    [oberser observeValueForKeyPath:key ofObject:self change:nil context:nil];
}

@end

Copy the code

The NSKVONotifying_MJPerson class object points to its own metaclass object

Objects that are not monitored by KVO

The object to listen on using KVO

Conclusion: Using KVO creates a subclass of MJPerson (NSKVONotifying_MJPerson) that changes the ISA of MJPerson to point to the new subclass, overrides setAge: and adds three new methods

Get method array

- (void)printMethodNamesOfClass:(Class)cls
{
    unsigned int count;
    // Get an array of methods
    Method *methodList = class_copyMethodList(cls, &count);
    
    // The name of the storage method
    NSMutableString *methodNames = [NSMutableString string];
    
    // Iterate over all the methods
    for (int i = 0; i < count; i++) {
        // Get the method
        Method method = methodList[i];
        // Get the method name
        NSString *methodName = NSStringFromSelector(method_getName(method));
        // The name of the splicing method
        [methodNames appendString:methodName];
        [methodNames appendString:@", "];
    }
    
    / / release
    free(methodList);
    
    // Prints the method name
    NSLog(@ % @ % @ "", cls, methodNames);
}

Copy the code

You can see the method name inside NSKVONotifying_MJPerson

NSKVONotifying_MJPerson type implementation
setAge: rewrite implementationFoundationthe_NSSetIntValueAndNotifymethods
class new Masking the internal implementation, hiddenNSKVONotifying_MJPersonClass object
dealloc new Clean up the site
_isKVOA new

_NSSetIntValueAndNotify Internal implementation

1. [self willChangeValueForKey:@"age"]
2.The originalsetterimplementation3. [self didChangeValueForKey:@"age"] didChangeValueForKey: internal invokes the observer observeValueForKeyPath: ofObject: change: context: methodCopy the code

A manual implementation of KVO that manually calls the willChangeValueForKey and didChangeValueForKey methods

_NSSet*ValueAndNotifyIncludes many basic data types

KVC

When a property is set using KVC, KVO is fired, even if there is no set method, – (void)willChangeValueForKey:(NSString *)key and – (void)didChangeValueForKey:(NSString *)key

Example: search order (attribute nameage) _age _isAge age isAge

NSNotification

  • Is the use ofObserver modelTo implement a mechanism for sending messages across layers
  • The delivery is one-to-many

The difference between notification and agency:

  • The agent isThe proxy patternImplemented, the notification isObserver modelImplementation of the
  • The agent is one-to-one and the notification is one-to-many