Efficient debugging of iOS

Efficient debugging of iOS

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:

  1. //Swift version
  2. func DLog(message: T, file: String = #file, method: String = # function , line: Int = #line) {
  3. #if DEBUG
  4. print( ", \(method) \(message)" )
  5. #endif
  6. }
  7.   
  8. //OC version
  9. #ifdef DEBUG
  10. #define DLog(fmt, ...) NSLog((@ " %s " fmt), [[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], __LINE__, __PRETTY_FUNCTION__, ##__VA_ARGS__);
  11. # else  
  12. #define DLog(...)
  13. #endif

eg:

  1. //Call the following method in AppDelegate.m
  2. - (void)applicationDidFinishLaunching:(UIApplication *)application{
  3. DLog(@ "hello world" );
  4. }
  5. //Print result: 2016-09-18 17:19:27.931 DateCell[2901:1622220] -[AppDelegate applicationDidFinishLaunching:] hello world

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:

  • Condition The condition to be set.
  • Ignore indicates the number of times the breakpoint is ignored before it becomes effective. For example, if no condition is set and Ignore is set to 4, the program stops at the breakpoint when i=4.
  • Action is the action to be performed at the program breakpoint (the code stops running after this action is executed, and the code at the breakpoint is not executed at this time). The action here is an LLDB statement, and LLDB will be introduced below.
  • Options When selected, the code will not stop after executing the Action, just like no breakpoint is set.

(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.

  1. brew update  
  2. brew install chisel

After the installation is complete, add the following content to ~/.lldbinit. If there is no ~/.lldbinit, create a new one.

  1. # ~/.lldbinit
  2. ...
  3. command script import /path/ to /fblldb.py

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:

  • View and modify views
  • View the properties of any object
  • Dynamically modify properties
  • Dynamically calling instance methods and class methods
  • View the network request process
  • Add simulated keyboard shortcuts
  • View system logs
  • Get any object from the heap
  • View files in the sandbox
  • View SQLite/Realm databases in the file system
  • Triggering 3D touch in the simulator
  • View all classes in your application
  • Quickly access commonly used classes, such as [UIApplication sharedApplication], the app delegate, the root view controller on the key window, and more.
  • Dynamically view the values ​​in NSUserDefaults

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

Recommend

Code Review from Scratch

[[156391]] This post is not about introducing the...

This software can extend the battery life of Android phones by 16%

iOS 9 has been released, and one of the highlight...

When people get old, do they really smell like old people?

People with life experience know that many elderl...

How SEO Got My App 150,000 Downloads

After reading this article, I have a question: th...

Do we need to worry about radiation in our daily lives?

When it comes to radiation, many people will thin...

Why are mobile phones getting more expensive?

I think the reasons can be roughly divided into t...

Shh! Here's a quick guide to surviving a drinking party

The wine table during the Chinese New Year can be...