Bugs are inevitable when writing code. Having some debugging skills can definitely improve your work efficiency and make bugs disappear. Here I will share with you some iOS debugging tips that I often use in my work. 1. Print The simplest and most basic debugging method is to print logs. Here are two encapsulated log printing codes:
eg:
2. Breakpoints (1) Normal breakpoints Normal breakpoints are the most commonly used in debugging. When the program reaches a breakpoint, it will pause. The setting method is very simple: just click on the left side of the code line where you want to set the breakpoint. Normal breakpoint (2) Conditional breakpoints (add conditions and other attributes to the breakpoints, temporarily called conditional breakpoints) Conditional breakpoints are based on ordinary breakpoints with added judgment conditions. When the program executes to the breakpoint and meets the set conditions, the breakpoint will be effective. The setting method is as follows: Open the breakpoint edit box Write the picture description here In the above settings, the condition of the conditional breakpoint is i==3, so in the entire loop the program will only stop executing at the breakpoint when i=3. In the Edit Breakpoint… window there are four input items:
(3)Exception BreakPoint When the program crashes when an exception breakpoint is set, Xcode will help us locate the location where the crash occurred. There is no abnormal breakpoint Setting abnormal breakpoints Steps to set exception breakpoints: Write the picture description here (4) Symbolic Breakpoint Symbolic breakpoints can be used to set breakpoints for a method or a method of a class. The functions implemented are as follows: Set a breakpoint in a method of a class Execute a breakpoint in a method The setup steps are as follows: Steps to set symbolic breakpoints 3. LLDB LLDB is the default debugger for Xcode. We can make the debugging process more flexible by executing LLDB commands. Xcode has a built-in LLDB debugging window. When the program reaches a breakpoint, you can enter LLDB commands to operate the debugging process. Common LLDB commands are as follows: 1. po (print object) output object, such as po [self view]; 2. p (print) is used to output basic types, such as p (int)[[[self view] subviews] count] outputs the number of subviews. 3. expr (expression) can dynamically execute the specified expression during debugging and print out the result. It is often used to modify the value of a variable during debugging. For example, as shown in the figure above, when the program first executes to a breakpoint Execute the following command: expr i=4 You will see the following output: (int) $0 = 4 Continue running the program, the program output information is: value:4 i==4 4. call Call means to call. In fact, the above po and p also have the function of calling. Therefore, call is generally only used when there is no need to display output or the method has no return value. We can set a breakpoint in viewDidLoad:, and then enter the following command when the program is interrupted: call [self.view setBackgroundColor:[UIColor redColor]] At this time, the background color of the view changes to red. 5. bt prints the call stack. Adding all prints the stack of all threads. I won't give a detailed example here. Interested friends can try it themselves. 6. fr v -R command to print out the raw information of the variable If you want to know more details, click here. http://lldb.llvm.org/tutorial.html 4. Chisel Chisel is Facebook's open source lldb command for interface debugging. Install Chisel is installed using homebrew. If you don't have homebrew installed, refer to homebrew.
After the installation is complete, add the following content to ~/.lldbinit. If there is no ~/.lldbinit, create a new one.
Restart Xcode and Chisel will be ready to use. Order Command Collection Students who are not good at English can read here. https://blog.cnbluebox.com/blog/2015/03/05/chisel/ 5. UI debugging tool: Reveal Reveal is definitely a great tool for debugging iOS interfaces. Once you use it, you will never be able to live without it. For an introduction to Reveal, you can visit its official website. Install Reveal is charged, and the genuine version is recommended. The following cracked version is for teaching purposes only and cannot be used in production environments. 1. Download Reveal from the official website (https://revealapp.com/) and install it; 2. Download the crack file (https://pan.baidu.com/s/1bNPhlO) Extraction password: 7×48; 3. For cracking steps, please refer to the instructions in the cracking file. 4. Open Reveal and start using it. The new version of Reveal may modify the verification logic. If the crack fails, use the genuine version. Configure in project Before using Reveal, you need to perform a simple configuration on the iOS project. Configuration method 1: Reveal can be imported using cocoa pods: import pod 'Reveal', '~> 1.3' in the Podfile. Configuration Method If you don't use cocoa pods, the configuration is a bit troublesome. Here is the detailed configuration process: 1. Use Xcode to open the project whose UI you want to view; 2. Open Reveal and click Help → Show Reveal Library in Finder; Write the picture description here 3. Drag Reveal.framework into the opened Xcode project and click Finish; Write the picture description here 4. Click Build Phases and delete Reveal.framework from Link Binary With Libraries; Remove Reveal.framework from Link Binary With Libraries 5. Then select Build Settings and type Other Linker Flags in the search bar. Select Other Linker Flags. Enter the following code in Other Linker Flags: -ObjC -lz -framework Reveal Configuring Other Linker Flags 6. Run the simulator, open Reveal, and connect to the simulator. Link Simulator That's it. Next, you can debug your interface using Reveal. Effect 6. Debugging toolset: FLEX FLEX is a set of tools for debugging in applications that Flipboard has developed. FLEX is integrated into applications as a third-party library. When using it, add the library to the project, and then call [[FLEXManager sharedManager] showExplorer]; to display the debugging toolbar for debugging. It provides the following functions:
It's awesome. You'll realize its power when you integrate FLEX into your project. Conclusion Six debugging methods are introduced above. During development, you can choose the most appropriate debugging method according to the specific situation. |
<<: iOS advanced page performance optimization
>>: Mobile terminal routing layer design
The transmissibility of infectious diseases has m...
[[156391]] This post is not about introducing the...
Xu Yong's Cognitive Behavioral Therapy (CBT) ...
Good news, good news. Just hold on for another we...
If we talk about the most painful skin problem, b...
March 14, 2023 The 13th Five-Year Plan for Nation...
How much does it cost to customize the Bayanzhuoe...
iOS 9 has been released, and one of the highlight...
People with life experience know that many elderl...
After reading this article, I have a question: th...
When it comes to radiation, many people will thin...
What methods can SEO promotion use to make the we...
Renren Video temporarily shuts down to clean up c...
I think the reasons can be roughly divided into t...
The wine table during the Chinese New Year can be...