Tricks Guide In the process of iOS project development, static analysis (Analyze), breakpoints (BreakPoint) and console (Console) are often used for code debugging. This article introduces "breakpoint debugging", a common debugging method in Xcode.
1. Introduction Xcode's breakpoint function is a common function for iOS developers. Click on the left column in the code editing area to create a breakpoint. Every time the program runs to the breakpoint, it will pause, making it easier for developers to debug. As shown below: At the same time, developers can use the tools marked with numbers in the figure above to assist breakpoint debugging. The functions of each tool are as follows:
In addition to the basic usage of breakpoints, developers can also use advanced breakpoint features: edit breakpoints, exception breakpoints, and symbolic breakpoints. 2. Edit breakpoints Double-click a breakpoint or right-click it and select Edit Breakpoint to open the breakpoint editing page. The page contains four parts: Condition, Ignore, Action, and Options. Let's introduce them one by one. 1. Condition Condition is a condition. Developers can set the conditions for triggering breakpoints in the Condition input box. For example, in a for loop, you can set the breakpoint to be triggered only when i==5 is satisfied. As shown below: 2. Ignore Ignore is the number of times to ignore. Developers can set the number of times to ignore this breakpoint in the Igore input box. For example, if Ignore is set to 5, the breakpoint will be triggered only when the breakpoint line is executed for the 6th time. 3. Action Action is the trigger action. You can add multiple Actions. After the breakpoint is triggered, the set Actions will be executed immediately. Action has 6 execution types, among which the more commonly used ones are Debugger Command and Log message, as shown below: 1) Debugger Command Debugger Command allows developers to set some LLDB commands to implement functions such as printing objects and modifying variables. As shown below: 2) Log Message Log Message provides a standard text output format. Developers can set the content to be output after the breakpoint is triggered according to the format. Among them, @exp@ indicates the content of output exp, %B indicates the name of the output breakpoint, and %H indicates the number of times the output breakpoint is executed. As shown below: 4. Options Options controls whether to automatically continue executing the program after executing the corresponding Actions of the breakpoint. After checking Options, the Debug interface will not be entered after the breakpoint is triggered. 3. Abnormal breakpoints (global breakpoints) Developers can easily create an exception breakpoint in Xcode. As shown below: After creating an exception breakpoint, a breakpoint named All Exceptions will be displayed, so it is often called a global breakpoint. When the program throws an exception, the exception breakpoint will be triggered, and most common errors will be located at the corresponding line of code by the breakpoint, which is very convenient for debugging. For example: after setting a global breakpoint, we write a piece of code that exceeds the bounds of an array. When running this code, the breakpoint will be set to the line that exceeds the bounds. As shown below: When the global breakpoint is removed and the program is run again, the out-of-bounds code is triggered and the breakpoint is set to main.m, but the out-of-bounds line is not located. As shown below: 4. Symbolic breakpoints Symbolic Breakpoint is a global breakpoint that can be set for a method (function). Developers can easily create a symbolic breakpoint, as shown below: Compared with ordinary breakpoints, the editing interface of symbolic breakpoints has two more input boxes: Symbol and Module. 1. Symbol Developers can set breakpoints to trigger methods/functions in the Symbol input box. As shown in the figure below, after setting a method/function in the Symbol, the breakpoint will be triggered when the program is run and the method is executed. Here we should pay attention to the writing of methods/functions in Symbol:
2. Module Developers can set the library where the function in the Symbol is located in the Module input box to avoid methods/functions with the same name in different libraries. By default, this field does not need to be filled in. 3. Condition and other functions The usage of the Condition function in the symbol breakpoint editing page, except for Symbol and Module, is the same as that of ordinary breakpoints. For example, if you set $arg3==nil && $arg4==nil in the Condition input box, the breakpoint will be triggered only when the first and second parameters are both nil. As shown below: Summarize Xcode's breakpoint debugging function is very powerful. Developers can make full use of breakpoint debugging and combine it with rich LLDB debugging commands to improve development efficiency to a great extent. [This article is an original article from 51CTO columnist 360 Technology, WeChat public account "360 Technology (id: qihoo_tech)"] Click here to read more articles by this author |
<<: Samsung Galaxy Fold review: An imperfect folding screen, a promising new form factor
>>: iOS 13 will also add a dark mode. Is the era of "everyone in black" coming?
Is it easy to be an agent of Jixi Points Mini Pro...
[[161368]] I'm a non-technical product manage...
The acquisition of 95% of the shares of TVS, a su...
Recommended new media traffic channels: 1. WeChat...
As the epidemic continues, many companies are not ...
1 Strange things happened on the train A while ag...
According to QuestMobile data, the online educati...
Acidic foods cause cancer, alkaline foods are hea...
If you have seen the movie "Dear", you ...
Among all the TV box products we have experienced...
Yesterday, the 15th meeting of the Conference of ...
Despite Google's fragmentation fix being rele...
It was somewhat unexpected that the 2024 Nobel Pr...
There is such a cow It was born in the Ulagai gra...