为什么系统调用UIApplicationDelegate的dealloc方法? [英] Why system call UIApplicationDelegate's dealloc method?

查看:181
本文介绍了为什么系统调用UIApplicationDelegate的dealloc方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下一个代码:

// create new delegate
MyCustomApplicationDelegate *redelegate = [[MyCustomApplicationDelegate alloc] init];
redelegate.delegate = [(NSObject<UIApplicationDelegate42> *)[UIApplication sharedApplication].delegate retain];

// replace delegate
[UIApplication sharedApplication].delegate = redelegate;

...
[UIApplication sharedApplication].delegate = redelegate.delegate;
[redelegate.delegate release];

在最后一行系统调用基础UIApplicationDelegate类的dealloc方法之后。
那么,为什么呢?我阅读了有关UIApplication的Apple文档,关于委托属性:

after last line the system called dealloc method of base UIApplicationDelegate class. So, why? I read Apple documentation about UIApplication, about delegate property:


@property(nonatomic,assign)id delegate

@property(nonatomic, assign) id delegate

讨论委托必须采用UIApplicationDelegate正式的
协议。 UIApplication分配并且不保留代理。

Discussion The delegate must adopt the UIApplicationDelegate formal protocol. UIApplication assigns and does not retain the delegate.

它清楚地表明UIApplication分配并且不保留代理。那么,为什么它会破坏我的基础委托呢?

It clearly says that UIApplication assigns and does not retain the delegate. So, why it destroy my base delegate?

推荐答案

UIApplication有一些不寻常的行为。第一次在UIApplication上设置委托属性时,将释放旧委托。每次在此之后,当您设置委托属性时,旧委托不会被释放。

UIApplication has some unusual behavior. The first time that you set the delegate property on UIApplication, the old delegate is released. Every time after that, when you set the delegate property the old delegate is not released.

UIApplication.h中委托属性的声明是:

The declaration for the delegate property in UIApplication.h is:

@property(nonatomic,assign) id<UIApplicationDelegate> delegate;

这意味着共享UIApplication永远不会在委托上调用retain或release。这对于委托模式来说是正常的:一个类通常不会保留其委托,因为这会导致一个保留循环。

This implies that the shared UIApplication will never call retain or release on the delegate. This is normal for the delegate pattern: A class normally doesn't retain its delegate because this results in a retain loop.

但在这种情况下,有一个不寻常的问题:谁拥有应用程序的第一个代表?在main.m中,对 UIAplicationMain()的调用隐式地分配并插入第一个委托,这使得该委托的保留计数为1.有人必须释放它但是没有上课,拥有它。要解决这个问题,无论何时第一次在UIApplication上设置新委托,它都会释放第一个委托。新代理由应用程序中的某个类分配和启动,因此您已拥有对新代理的引用。 UIApplication不保留或释放新代表。

But in this case, there's an unusual problem: Who owns the app's first delegate? In main.m the call to UIAplicationMain() implicitly allocs and inits the first delegate, which leaves that delegate with a retain count of 1. Someone has to release that but there's no classes around to own it. To solve this, whenever you set a new delegate on UIApplication for the first time, it releases that first delegate. The new delegate was alloced and initted by some class in your app, so you already own a reference to the new delegate. UIApplication doesn't retain or release the new delegate.

这篇关于为什么系统调用UIApplicationDelegate的dealloc方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆