KVO
- Through print
The monitored instance object
theisa
Pointer, which can be found, of the listening instance objectisa
Instead of pointing to class objects, I point toNSKVONotifying_MJPerson
NSKVONotifying_MJPerson
Is the use ofRuntime
A dynamically created class, isMJPerson
A subclass of
NSKVONotifying_MJPerson pseudo code
- through
[self.person1 methodForSelector:@selector(setAge:)]
Gets the address of the method implementation - Through the LLDB command
p (IMP)0x1069189e4
Gets 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 | implementationFoundation the_NSSetIntValueAndNotify methods |
class |
new | Masking the internal implementation, hiddenNSKVONotifying_MJPerson Class 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*ValueAndNotify
Includes 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 of
Observer model
To implement a mechanism for sending messages across layers - The delivery is one-to-many
The difference between notification and agency:
- The agent is
The proxy pattern
Implemented, the notification isObserver model
Implementation of the - The agent is one-to-one and the notification is one-to-many