QGVAPWeakProxy.m 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // QGVAPWeakProxy.m
  2. // Tencent is pleased to support the open source community by making vap available.
  3. //
  4. // Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
  5. //
  6. // Licensed under the MIT License (the "License"); you may not use this file except in
  7. // compliance with the License. You may obtain a copy of the License at
  8. //
  9. // http://opensource.org/licenses/MIT
  10. //
  11. // Unless required by applicable law or agreed to in writing, software distributed under the License is
  12. // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  13. // either express or implied. See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #import "QGVAPWeakProxy.h"
  16. @implementation QGVAPWeakProxy {
  17. __weak id _target;
  18. }
  19. - (instancetype)initWithTarget:(id)target {
  20. if (self = [super init]) {
  21. _target = target;
  22. }
  23. return self;
  24. }
  25. + (instancetype)proxyWithTarget:(id)target {
  26. return [[QGVAPWeakProxy alloc] initWithTarget:target];
  27. }
  28. // 1. 快速消息转发
  29. - (id)forwardingTargetForSelector:(SEL)aSelector {
  30. return _target;
  31. }
  32. // 2. 如果<1>返回nil,到标准消息转发处理,如果不处理为Crash:unrecognized selector. 这里我们直接返回空指针地址.
  33. - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
  34. return [NSObject instanceMethodSignatureForSelector:@selector(init)];
  35. }
  36. - (void)forwardInvocation:(NSInvocation *)anInvocation {
  37. void *null = NULL;
  38. [anInvocation setReturnValue:&null];
  39. }
  40. - (BOOL)respondsToSelector:(SEL)aSelector {
  41. return [_target respondsToSelector:aSelector];
  42. }
  43. @end