When you develop an iOS app, you sometimes need to simulate memory warning notifications to test your iOS app in high memory pressure conditions. Xcode simulator just provides such a tool for you to trigger memory warning notifications. Then you can test your source code in ViewController class’s didReceiveMemoryWarning function. This article will tell you how to do it.
1. Simulate Memory Warning Notification In Xcode iOS Simulator Steps.
- First, create an Xcode project.
- Then override and write some code in your ViewController class’s didReceiveMemoryWarning function like below. The class file name is ViewController.swift.
override func didReceiveMemoryWarning() { print("view receive memory warning.") }
- Now start an iOS simulator to run your Xcode iOS app project.
- When the iOS simulator startup, click it to focus on it, then click Debug —> Simulate Memory Warning menu item at the simulator top menu bar. This menu item will send the memory warning notification in the iOS simulator, and your app will receive the memory warning notification, and then run the code in ViewController class’s didReceiveMemoryWarning function.
- Now you can see the output text in Xcode debug console as below.
view receive memory warning.
- If you do not know how to display the Xcode debug console and see the output text, please read the article How To Debug And Get Error Messages In Xcode Step By Step