在我的应用程序中,我做了这样的事情,
AppDelegate *appDelegate = [[UIApplication sharedApplication] 委托]; //一些代码 appDelegate = nil;
但是,应用程序运行良好,不会发出警告或崩溃。
有人可以解释一下吗?谢谢。
在我的应用程序中,我做了这样的事情,
AppDelegate *appDelegate = [[UIApplication sharedApplication] 委托]; //一些代码 appDelegate = nil;
但是,应用程序运行良好,不会发出警告或崩溃。
有人可以解释一下吗?谢谢。
First you made appDelegate point to [[UIApplication sharedApplication] delegate]. Then you made it point to nowhere. But [[UIApplication sharedApplication] delegate] remains the same. You did nothing with it. You didn't even touch it.
What you probably are thinking of is:
[[UIApplication sharedApplication] setDelegate:nil];
But it also shouldn't produce warnings or crashes. It should only result in not calling app delegate methods since messages sent to nil do nothing.
[[UIApplication sharedApplication] delegate]会给你一个指向你的应用程序委托的指针,它是在你创建项目时自动创建的。所以设置 appDelegate = nil,只是将指向应用程序委托的指针设置为 nil。不对应用程序委托做任何事情,它是自动创建的..
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
使用这一行,它会返回一个 appDelegate 的共享实例。
[UIApplication sharedApplication]--- 允许访问单例
delegate方法返回一个指向应用程序委托的指针。当您在完成所有工作后将 nil 分配给 appDelegate 时。为什么会崩溃?如果您在分配 nil 值后尝试调用方法,则可能是这样。那不管用。
您将appDelegate指针设置为nil不是应用程序的委托。写完这段代码,[[UIApplication sharedApplication] delegate]还是不行nil。