Xcode includes a set of tools to help developers analyze, debug, and profile their app. Every developer should use them so their iOS app has the best chance of being accepted.
If you have an app you plan to submit to the Apple store you, of course, want to make sure it's robust. That is, that it won't crash, leak or use too much memory. That it will run in poor or nonexistent network conditions, and always be responsive to the user. Before you submit your app you should put together a unit test plan and then run the suite of tools available with the latest version of Xcode.
I will introduce the tools but, because there are so many, and each one deserves its own tutorial, I won't go into much detail. References at the end of the article will point you to where to go to find out more.
Two tools you should run while you are developing your app are the Clang Static Analyzer and the Debugger. You run the Static Analyzer to prevent bugs and the Debugger to step through your code to find bugs.
Static AnalyzerRun the Static Analyzer by selecting Analyze from the Xcode Product menu (key shortcut Shift-Command-B). After running the Analyzer, a list of potential problems appears in the Navigator pane. Select one and then view the source in the Editor pane. Figure A shows a memory leak, because when the responseData instance variable is assigned, it is both allocated, with alloc, and retained.
The fix is to replace the assignment with a method that doesn't increment the retain count:
self.responseData = [NSMutableData dataWithCapacity:[data length]];
The Debugger is one of the most important developer tools in any programming environment. Xcode gives you a choice of two Debuggers, the reliable GDB and the newer and faster LLDB.To run the Debugger, select Edit Scheme... from the Xcode Product Menu. The following dialog appears. (Figure B)
Selecting the Debug scheme turns off compiler optimizations and turns on a Debug macro in your project's Build Settings.
Before you run your app, you should set your breakpoints and reveal the debug area below the source editor by Selecting Debug Area | Show Debug Area from the View menu.As Figure C shows, the Debug Area has a Local variable pane that allows you to inspect the values of variables in memory as you step through the code.
Instruments are profiling tools provided with the Xcode IDE for debugging, performance analysis, and testing. With Instruments you can profile:
- General and heap memory usage and check for leaked memory
- Real-time CPU, memory, disk, energy, and network usage
- Threads, network, and energy usage
- I/O system and thread activity
- File I/O and Core Data activity
- "Over-released" objects known as Zombies
- The amount of time the app spends in a routine
- Graphics library activity
You can also automate user tasks and you can create your own custom profiler with the Blank template by dragging instruments from the Library to the Instruments list. For example, time activity could be cross-referenced with memory usage.Open an Instrument template (Figure D) by selecting Open Developer Tool | Instruments from the Xcode menu, or select Profile from the Product menu to profile your current project.
Generally you should select an iOS template to profile with an attached iOS device. However, some of the instruments that could be useful, depending on your app, only work with the simulator. These include the Zombies, File System and Core Data instruments.
View the "Instruments User Guide" by selecting it from the Instruments Help menu. It's also available online at the URL reference below.
Apple also offers some advanced tools that must be downloaded outside of Xcode. Select Open Developer Tool | More Developer Tools from the Xcode menu. Safari will open the Developer website. Sign in and you will see a list of downloads. You can select tools for hardware I/O, audio, building from the command line, etc.Download and install the latest version of the Hardware I/O tools. Included in this set of tools is the Network Link Conditioner, which is installed as a pane in System Preferences. (Figure E)
Open the Systems Preferences and you'll find the Network Link Conditioner in the "Other" section. Set the network profile to 3G, Lossy Network, as above, and turn the conditioner on. You are now ready to profile your app with Instruments in the simulator and see how it handles a lossy network.