一、官方文档
An abstract superclass defining an API for objects that act as stand-ins for other objects or for objects that don’t exist yet.
定义对象API的抽象超类,该对象充当其他对象或尚不存在的对象的替身。
通常,发送给代理的消息被转发到实际对象,或者导致代理加载(或将自身转换为)实际对象。NSProxy的子类可以用来实现透明的分布式消息传递(例如,NSDistantObject),或者用于延迟实例化那些创建起来代价昂贵的对象。
作为一个抽象类,它不提供初始化方法,并且在接收到它不响应的任何消息时会引发异常。因此具体的子类必须提供一个初始化或创建方法,并覆盖forwardInvocation(:)和methodSignatureForSelector:方法来处理它自己没有实现的消息。子类的forwardInvocation(:)实现应该执行处理调用所需的任何操作,比如通过网络转发调用,或者加载实际对象并将调用传递给它。methodSignatureForSelector:需要为给定消息提供参数类型信息;一个子类的实现应该能够确定它需要转发的消息的参数类型,并且应该相应地构造一个NSMethodSignature对象。更多信息,请参见NSDistantObject、NSInvocation和NSMethodSignature类规范。
二、接口文档
/* NSProxy.h
Copyright (c) 1994-2019, Apple Inc. All rights reserved.
*/
#import <Foundation/NSObject.h>
@class NSMethodSignature, NSInvocation;
NS_ASSUME_NONNULL_BEGIN
NS_ROOT_CLASS
@interface NSProxy <NSObject> {
__ptrauth_objc_isa_pointer Class isa;
}
+ (id)alloc;
+ (id)allocWithZone:(nullable NSZone *)zone NS_AUTOMATED_REFCOUNT_UNAVAILABLE;
+ (Class)class;
- (void)forwardInvocation:(NSInvocation *)invocation;
- (nullable NSMethodSignature *)methodSignatureForSelector:(SEL)sel NS_SWIFT_UNAVAILABLE("NSInvocation and related APIs not available");
- (void)dealloc;
- (void)finalize;
@property (readonly, copy) NSString *description;
@property (readonly, copy) NSString *debugDescription;
+ (BOOL)respondsToSelector:(SEL)aSelector;
- (BOOL)allowsWeakReference API_UNAVAILABLE(macos, ios, watchos, tvos);
- (BOOL)retainWeakReference API_UNAVAILABLE(macos, ios, watchos, tvos);
// - (id)forwardingTargetForSelector:(SEL)aSelector;
@end
NS_ASSUME_NONNULL_END
func finalize()
The garbage collector invokes this method on the receiver before disposing of the memory it uses.
func forwardInvocation(NSInvocation)
Passes a given invocation to the real object the proxy represents.
methodSignatureForSelector:
Raises NSInvalidArgumentException. Override this method in your concrete subclass to return a proper NSMethodSignature object for the given selector and the class your proxy objects stand in for.
NSInvocation
An Objective-C message rendered as an object.
iOS给某个对象的发消息方式有两种:
- performSelector:withObject
- 通过创建NSInvocation调用某个对象的方法
代码实现:
- (void)viewDidLoad {
[super viewDidLoad];
//NSInvocation;用来包装方法和对应的对象,它可以存储方法的名称,对应的对象,对应的参数,
/*
NSMethodSignature:签名:再创建NSMethodSignature的时候,必须传递一个签名对象,签名对象的作用:用于获取参数的个数和方法的返回值
*/
//创建签名对象的时候不是使用NSMethodSignature这个类创建,而是方法属于谁就用谁来创建
NSMethodSignature*signature = [ViewController instanceMethodSignatureForSelector:@selector(sendMessageWithNumber:WithContent:)];
//1、创建NSInvocation对象
NSInvocation*invocation = [NSInvocation invocationWithMethodSignature:signature];
invocation.target = self;
//invocation中的方法必须和签名中的方法一致。
invocation.selector = @selector(sendMessageWithNumber:WithContent:);
/*第一个参数:需要给指定方法传递的值
第一个参数需要接收一个指针,也就是传递值的时候需要传递地址*/
//第二个参数:需要给指定方法的第几个参数传值
NSString*number = @"1111";
//注意:设置参数的索引时不能从0开始,因为0已经被self占用,1已经被_cmd占用
[invocation setArgument:&number atIndex:2];
NSString*number2 = @"啊啊啊";
[invocation setArgument:&number2 atIndex:3];
//2、调用NSInvocation对象的invoke方法
//只要调用invocation的invoke方法,就代表需要执行NSInvocation对象中制定对象的指定方法,并且传递指定的参数
[invocation invoke];
}
- (void)sendMessageWithNumber:(NSString*)number WithContent:(NSString*)content{
NSLog(@"电话号%@,内容%@",number,content);
}
三、NSProxy使用
1、避免循环引用
#import <Foundation/Foundation.h>
@interface WeakProxy : NSProxy
+ (id)proxyWithTarget:(id)target;
- (instancetype)initWithTarget:(id)target;
@end
#import "WeakProxy.h"
@interface WeakProxy ()
@property (nonatomic, weak) id target;
@end
@implementation WeakProxy
- (instancetype)initWithTarget:(id)target {
_target = target;
return self;
}
+ (id)proxyWithTarget:(id)target {
return [[self alloc] initWithTarget:target];
}
- (void)forwardInvocation:(NSInvocation *)invocation {
SEL selector = [invocation selector];
if ([_target respondsToSelector:selector]) {
[invocation invokeWithTarget:_target];
}
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel {
return [_target methodSignatureForSelector:sel];
}
@end
let timer = Timer(timeInterval: interval, target: WeakProxy.proxy(withTarget: self), selector: #selector(timerCallback), userInfo: nil, repeats: repeats)
RunLoop.current.add(timer, forMode: RunLoop.Mode.common)
3、实现多继承
网友评论