iOS - ARC dealloc
YYKit/YYTextView.m 2014-2027
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIPasteboardChangedNotification object:nil];
[[YYTextKeyboardManager defaultManager] removeObserver:self];
[[YYTextEffectWindow sharedWindow] hideSelectionDot:_selectionView];
[[YYTextEffectWindow sharedWindow] hideMagnifier:_magnifierCaret];
[[YYTextEffectWindow sharedWindow] hideMagnifier:_magnifierRanged];
[YYTextDebugOption removeDebugTarget:self];
[_longPressTimer invalidate];
[_autoScrollTimer invalidate];
[_selectionDotFixTimer invalidate];
}
///< NSTimer使用第三方中介强引用
....{
_longPressTimer = [NSTimer timerWithTimeInterval:kLongPressMinimumDuration
target:[YYWeakProxy proxyWithTarget:self]
selector:@selector(_trackDidLongPress)
userInfo:nil
repeats:NO];
[[NSRunLoop currentRunLoop] addTimer:_longPressTimer forMode:NSRunLoopCommonModes];
}
1.系统的通知的观察者/KVO观察者;
2.移除强引用self的委托,如:UIWebViewDelegate;
3.资源销毁,如:NSTimer;
4.非NSOject对象销毁,如CoreFoudation;
在《Effective Objective-C 2.0》书建议,不要在dealloc中给其他对象发消息。
附带知识点
1.YYWeakProxy第三方中介
NSProxy 推荐博客
YYWeakProxy主要使用runtime的消息转发机制,NSProxy复制类(可以变成成为任何对象,多继承,多替换)。
2.函数签名
函数签名包括函数名、参数类型、参数个数、参数顺序。
3.函数重载
多个相同函数名,不同的参数个数或者类型的形式叫做函数重载。
4.消息转发机制
Objective-C动态消息型语言,它将数据类型等工作推迟到运行时(runtime),其中调用方法本质就是向对象发送消息,根据SEL在对象的方法列表->父类的方法列表进行查找,如果都找不到就会启动消息转发。
- (void)forwardInvocation:(NSInvocation *)invocation;
- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel;
- (NSMethodSignature *)methodSignatureForSelector:(sel) {
NSMethodSignature * signature = nil;
if([_weakTarget methodSignatureForSelector:sel]) {
signature = [_weakTarget methodSignatureForSelector:sel];
}else {
signature = [super methodSignatureForSelector:sel];
}
return signature;
}
- (void)forwardInvocation:(NSInvocation *)invocation {
void * null = NULL;
[invocation setReturnValue:&null];
}

浙公网安备 33010602011771号