Frage

In my app, i have done something like this,

 AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
 //some code
 appDelegate = nil;

But, App runs fine and does not give warning or crash.

Can anybody explain this? Thanks.

War es hilfreich?

Lösung

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.

Andere Tipps

[[UIApplication sharedApplication] delegate] will give you a pointer to your app delegate,the one that was automatically created when you made the project. So setting appDelegate = nil, just sets the pointer to the app delegate to nil. Does not do anything to app delegate, the one that is automatically created..

 AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

Using this line it return you a shared instance of appDelegate.

[UIApplication sharedApplication] --- gives access to the singleton

delegate method returns a pointer to the app delegate. and when you assign nil to appDelegate after finishing all works. Why will it crash? Pobably if you will try to call a method after assigning it nil value. That will not work.

You are setting your appDelegate pointer to nil not the delegate of the app. After you write this code, [[UIApplication sharedApplication] delegate] would still not be nil.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top