Writing code inevitably bugs. Storing some debugging skills will definitely improve your productivity and make bugs invisible. I'll share some of the iOS debugging skills I used to work with you.
1. Printing
The simplest, basic debugging method is to print the log. Post a two-segment packaged log print code:
1 //Swift version2Func dlog<t> (message:t, file:string = #file, method:string = #function, Line:int =#line) {3 #ifDEBUG4Print"<\ ((file as NSString). Lastpathcomponent): \ (line), \ (method) \ (message)")5 #endif6 }7 8 //OC Version9 #ifdefDEBUGTen #defineDLog (FMT, ...) NSLog (@ "<%s:%d>%s" FMT), [[[NSString stringwithutf8string:__file__] lastpathcomponent] utf8string], __LINE__ , __pretty_function__, # #__VA_ARGS__); One #else A #defineDLog (...) - #endif
1 // call the following method in APPDELEGATE.M 2 -(void) applicationdidfinishlaunching: (uiapplication *) application{3 DLog (@ "HelloWorld"); 4 }5// print Result: 2016-09-18 17:19:27.931 datecell[2901:1622220] <APPDELEGATE.M: 54>-[appdelegate applicationdidfinishlaunching:] Hello World
2. Breakpoints (1) Normal breakpoint
Normal breakpoints are most commonly used in debugging. When the program runs to a breakpoint, it pauses to run. The Setup method is simple: Click to the left of the line of code where you want to set the breakpoint.
Normal breakpoint (2) Conditional breakpoint (add condition and other properties for breakpoint, call it conditional breakpoint)
Conditional breakpoints are added to the normal breakpoint based on the judgment condition. The breakpoint is valid when the program executes to a breakpoint and satisfies the set condition. The Setup method is as follows:
Open Breakpoint edit box
Write a picture description here
In the above setting, the condition of a conditional breakpoint is i==3, so the program stops executing at the breakpoint only when i=3 is in the entire loop.
In Edit breakpoint ... The following four entries are in the window:
- Condition Sets the condition.
- Ignore indicates how many times a breakpoint is ignored after it is valid. For example: Without setting a condition, ignore is set to 4, then the program stops at the breakpoint when i=4.
- action at the program breakpoint (after performing this operation, the code stops running, and the code at the set breakpoint has not been executed). Here the operation is LLDB statement, about LLDB will be described below.
- When Options is selected, the code does not stop after the action is executed, as if the breakpoint was not set.
(3) Abnormal Breakpoint Exception Breakpoint
When the program crash when the exception breakpoint is set, Xcode helps us navigate to where the crash is generated.
Case with no exception breakpoint
To set an exception breakpoint
To set an exception breakpoint step:
Write a picture description here (4) Symbol Breakpoint Symbolic Breakpoint
A symbolic breakpoint can set a breakpoint for a method or a method of a class. The functions implemented are as follows:
One method of a class sets a breakpoint
To execute a breakpoint in a method
The setup steps are as follows:
Sign breakpoints set Step 3. Lldb
LLDB's Xcode default debugger, we make the debugging process more flexible by executing the LLDB command.
Xcode inline lldb Debug window. After the program executes to the breakpoint you can enter the LLDB command to manipulate the debugging process.
Lldb Debug Window
Lldb common commands are as follows:
1, PO (Print object) Output object, such as po [self view];
2, p (print) is used to output the base type, such as P (int) [[[Self view] subviews] count] Output the number of child views.
3, expr (expression) can execute the specified expression dynamically at debug time and print the result. Often used to modify the value of a variable during debugging. As shown, the first time the program executes to a breakpoint
Execute the following instruction: expr i=4
You will see the following output: (int) $4
Continue to run the program, the program output information is: Value:4 i==4
4, call calls is the meaning of the invocation. In fact, the above PO and P also have the function of calling. Therefore, call is typically used only when no display output is required, or when the method has no return value. We can set a breakpoint in Viewdidload: And then enter the following command when the program breaks: call [Self.view Setbackgroundcolor:[uicolor Redcolor]] The background color of the view changes to red.
5, BT print Call stack, plus all to print all thread stacks. Not detailed examples, interested friends can try it on their own.
6, Fr v-r command to print out the variable's unprocessed stale information
If you want to know more details, poke here.
4. Chisel
Chisel is Facebook's Open source Lldb command for interface debugging.
Installation
Chisel use homebrew to install, if you have not installed homebrew, refer to homebrew.
Chisel use homebrew to install, if you have not installed homebrew, refer to homebrew.
updatebrew install chisel
After the installation is complete, add the following in ~/.lldbinit, and no ~/.lldbinit will be created.
# ~/.lldbinit...command script import /path/to/fblldb.py
Restart Xcode, at which point the Chisel is ready to use.
Command
Command Collection
English is not good students can see here.
5. Interface Debugging Tool: Reveal
Reveal is definitely the iOS interface debugging tool. Just use it for the last time and you'll be without it. For reveal introduction can go to the official website to understand.
Installation
Reveal is a charge, the promotion of genuine. The following cracked version is for teaching only and is not intended for use in production environments.
1, download reveal and install from official website;
2, download crack file extract password: 7x48;
3, the cracking procedure see the use of the cracked file instructions.
4, open reveal, start using. The new version of reveal may modify the verification logic, if not cracked successfully then use the genuine bar.
Configuring in Projects
The iOS project needs to be simply configured before using reveal, with the following configuration steps:
1. Use Xcode to open the project for which you want to view the UI;
2, open Reveal, click Help→show Reveal Library in Finder;
Write a picture description here
3. Drag the reveal.framework into the open Xcode project. and click Done;
Write a 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 check build Settings Enter other Linker Flags in the search field. Select Other Linker Flags
Enter the following code in the other Linker flags: -objc-lz-framework Reveal
Configure other Linker Flags
6, Run Simulator, open reveal, link simulator.
Link simulator
This is the most accomplished. Next you will be able to debug your interface via reveal.
Effect 6. Debug Toolset: FLEX
Flex is a set of Flipboard open source tools that are debugged in your app. Flex is integrated in the application as a third-party library, used to add the class library to the project, and then debugged with a call to [[FLEXManager sharedManager] showExplorer]; display the toolbar for debugging.
It provides the following features:
- View, modify views
- View the properties of any object
- Dynamic Modification Properties
- Dynamic invocation of instance methods and class methods
- View the network request process
- Add a simulated keyboard shortcut
- View System logs
- Get any object from the heap
- Viewing files in a sandbox
- View the Sqlite/realm database in the file system
- Triggering 3D touch in the simulator
- See all the classes in your app
- Quick access to commonly used classes such as [UIApplication Sharedapplication], the app delegate, the root view controller on the key window, and more.
- Dynamically view the values inside the Nsuserdefaults
It's a blast day. When you integrate flex into your project, you'll recognize its power.
Conclusion
Six kinds of debugging methods are described above, and the most suitable debugging method can be chosen according to the specific situation when developing. If Ben Boven is helpful to you, send a favor.
Wen/hejunm (author of Jane's book)
Original link: http://www.jianshu.com/p/a4ae30a10fb8
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".
iOS efficient debugging