Ios sdk: IOS debugging skills

Source: Internet
Author: User

Why does your array contain 3 items instead of 5 items? Why is your game running slowly? These are related to debugging. debugging is an essential part of the development process. This article lists some important debugging functions (of course not comprehensive) that can help you solve bugs in less time.

This article mainly includes three aspects: Use console to check app status Logging and mastering nslog Use the lifecycle of an object to track memory usage.  Use console to check app statusThe small black box at the bottom of xcode is a good friend during debugging. It can output log information, error information, and other useful things to help you track errors. In addition to the information directly output by logs, during programming, we can also stay at certain breakpoints to check multiple aspects of the app. Conditional breakpointI suppose you know how breakpoints works (if you don't know, you may know after reading this article !) It is very valuable for a program to hit a breakpoint at a specific point in time, but it is painful to make the object equal to a fixed value through a loop or recursive function. In this case, we can use conditional breakpoints! A conditional breakpoint is a breakpoint with a conditional expression. Only when this condition is met Will the program pause. Suppose we only want to break a breakpoint when the object is in a specific State, or hit a breakpoint in the nth iteration loop. Click 'gutter 'in xcode editor to add a breakpoint. Right-click the breakpoint and select "Edit breakpoint" to set specific conditions. Conditional breakpoints are interrupted only when a specific situation occurs. You can provide a condition (such as I = 12) or the number of times the breakpoint should be ignored. In addition, you can add an action that can automatically occur based on the breakpoint, such as a debugger command --- print a value. Tip: the keyboard shortcut for adding/deleting breakpoints is command + \. Another important breakpoint skill is to add an exception breakpoint (exception breakpoint ). When an exception occurs, xcode will basically automatically go to The autorelease pool of the main method. By setting an exception breakpoint, you can locate the specific code line that causes the exception breakpoint. How do I add an exception breakpoint? 1. Open the exception breakpoint tab (command + 6); 2. Select the "+" button in the lower-left corner of the window; 3. Select the button and add 'exception breakpoint '. In this way, when xcode encounters an exception, a breakpoint will occur where the Exception Code is caused. Manually print from the consoleTheoretically, it will show the status of all values in the current environment; in fact, sometimes there will be bugs, and no values will be listed or you will not update them when performing single-step debugging. Generally, we add a specific breakpoint to the app code to view the object status through the 'variables view' provided by xcode (this view is next to the console at the bottom of xcode. Theoretically, it can display the status of all values related to the current context. In fact, sometimes there may be a small problem. Related values are not listed or related updates are not performed. However, we can use some useful console commands to check specific objects. Enter 'po' in the console to obtain the real-time information of a breakpoint. (When processing scalar values, we can use 'P') This is useful when we view an existing object (NIL is printed if the object does not exist ), determine the value of the object, find out the information of the array/dictionary runtime, or even compare the two objects. Because this command prints the memory address of the related object, you can print the two objects you think should be the same to see if their memory address is the same. Another useful command, but the hidden command is recursivedescription. You can simply use it to check the view. Call recursivedescription in view to print its inheritance relationship. Valid LoggingSometimes, at a specific time of the debugging program, we want to print the message to the console. In this case, the 'nslog' function allows us to print any output to the console. In this case, you can use the nslog function to print any output to the console. This function is useful when no breakpoint is used. The format of nslog compliance is the same as that of the [nsstring stringwithformat] method. (You can see from below) tip: here you can see Apple's string formatting information about objective-C: String programming guide  NslogNslog is very useful. We need to implement it intelligently. Everything printed from nslog is converted into code, which can be seen by anyone. Connect the device to your computer, open the Organiser in xcode, And you can view each log on the console, which has a great impact. Think about it. You want to print some confidential algorithm logic or user passwords to the console. Because of this, if Apple finds that too much content is output to the console in the production build, your application may be rejected by Apple. Fortunately, here is the simplest way to perform log-through a macro, nslog only works during debug build. Add this function to the header files that can be accessed globally. In this way, you can use log as much as possible, and the log-related code is not included in the production. The following code: 1. # ifdef debug2. # define dmlog (...) nslog (@ "% S % @", _ pretty_function __, [nsstring stringwithformat :__ va_args _]) 3. # else4. # define dmlog (...) do {} while (0) If you use dmlog, it can only be printed during debug build. _ Pretty_function _ can also help print the name of the function where the log is located. Next step Nslog is powerful, but it has many limitations:1. Logs can only be printed locally. 2. Hierarchical logs (for example, dangerous logs or warnings) are not supported. 3. nslog is very slow and greatly reduces the program running efficiency when processing a large number of logs. We recommend two frameworks to avoid nslog restrictions: • cocoa lumberjack-one of the well-known common cocoa log frameworks, which is difficult to learn but powerful. • Snlog-an alternative to nslog. Lifecycle of a trail objectAlthough the automatic reference counting (ARC) has made memory management simple, time-saving, and efficient, tracking important events in the object's life-cycles is still very important. After all, arc does not completely eliminate the possibility of Memory leakage, or tries to access a release object. For this purpose, we can use some processing methods and tools to help us stare at what objects are doing. Important log eventsThere are two important methods in the life-cycle of the objective-C object: init and dealloc, it is a good choice to log the events called by these two methods to the console-you can observe the beginning of the object's life through the console. More importantly, you can ensure that the object is released. 1. -(ID) init2. {3. self = [Super init]; 4. if (Self) 5. {6. nslog (@ "% @: % @", nsstringfromselector (_ cmd), Self); 7 .} 8. return self; 9 .} 10. -(void) dealloc11. {12. nslog (@ "% @: % @", nsstringfromselector (_ cmd), Self); 13 .} Static analyzer and Inspector (checker)Xcode also has two tools to help us clean up code and reduce the chance of code errors. Static analyzer is a great tool for improving code for xcode. For example, if no objects have been used and no release objects have been detected (for core Foundation objects, arc still has such problems ). Select 'anlayze' from the product menu to view related suggestions. The checker is a very powerful set of tools. It not only checks the memory usage of programs from different angles, file System usage (add, delete, modify, etc.), and even provides automatic UI interaction methods. You can select 'profile 'from the product menu to view these checkers. Selecting 'profile 'opens an instrument window, where you can select a configuration template for running. The most common templates are zombies (which will be discussed later), activity monitor, and leaks. Leaks may be the most useful template for capturing memory leaks when running programs. Zombies is your friendAlthough it is difficult to encounter an uncomfortable exc_bad_access error where there is an arc, this error will still happen under certain conditions. When processing a uipopovercontroller or core Foundation object, we can access an object that has been release. Generally, an object in the release memory will be destroyed. However, when zombies is enabled, only the object is marked as release. In fact, this object remains in the memory. When we access a zombie object, xcode tells us that the object being accessed is an object that should not exist. Because xcode knows what this object is, it allows us to know where it is and when it occurs. There are two ways to find the zombies object. Use the zombie configuration template in the checker or enable the zombie diagnostic option in the 'run' build option. Click the scheme name next to the stop button, select 'edit Scheme ', click the diagnostic tab, and select 'Enable zombie objects '. Note: zombie can only be used in simulator debugging and cannot be used on a real machine. Note that zombie mode debugging is only applicable to simulators and cannot be used on real devices. SummaryI hope the above content can help you debug your app more efficiently, all of which are to save time for bug fixing, so that developers can spend time on more important things, or create a great application. The list above is definitely not a comprehensive list, and there are many methods we haven't discussed, such as remote control bug reports, crash reports, and more. I hope you can share more with me. Original article: http://www.cocoachina.com/newbie/basic/2013/0517/6225.htmlby yytong
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.