Sarcasm Sarcasm - 6 months ago 40
Swift Question

Do I need 'override func didReceiveMemoryWarning() {}' in my project?

I got an error here

override func didReceiveMemoryWarning() {


The error: Method does not override any of it's superclass.

The question:

Does the code
override func didReceiveMemoryWarning()
has to be in my project?

Sorry for my bad english.

Answer

No, you don't. Delete it until you have a need for it.

Shocking, I know, because it's recommended and it's in the template. But in all my years of iOS development, I've only actually had something useful to put in there maybe four or five times (and most of those were back in the iPhone 3GS days when memory was more limited). Frankly, for new developers, I'd just leave it out until you're ready to worry about optimizing resource utilization at that level. I think it creates more confusion than it solves. You definitely shouldn't put it in there unless you also perform tests to make sure your memory warning code works.

As Alessandro notes, this method is only applicable for UIViewController subclasses. (For other types, you can observe UIApplicationDidReceiveMemoryWarningNotification.) But you shouldn't just leave it in there because it's part of the template or because someone told you you should have it. If you don't have any meaningful memory to release (and honestly, view controllers seldom should), then you should get rid of the override.