iOS efficient debugging

Source: Internet
Author: User

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:

Swift version

Func dlog<t> (message:t, file:string = #file, method:string = #function, Line:int = #line) {

#if DEBUG

Print ("<\ (file as NSString). Lastpathcomponent): \ (line), \ (method) \ (message)")

#endif

}

OC version

#ifdef DEBUG

#define DLOG (FMT, ...) NSLog (@ "<%s:%d>%s" FMT), [[[NSString stringwithutf8string:__file__] lastpathcomponent] utf8string], __LINE__ , __pretty_function__, # #__VA_ARGS__);

#else

#define DLOG (...)

#endif

eg

Call the following method in APPDELEGATE.M

-(void) applicationdidfinishlaunching: (uiapplication *) application{

DLog (@ "Hello World");

}

Printed results: 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.

(2) Conditional breakpoint (add condition and other attributes 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:

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.

To set an exception breakpoint step:

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

The setup steps are as follows:

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

Brew Update

Brew 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

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.

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;

3. Drag the reveal.framework into the open Xcode project. and click Done;

4, click Build phases and delete 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

6, Run Simulator, open reveal, link simulator.

This is the most accomplished. Next you will be able to debug your interface via reveal.

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, and the class library is added to the project when used, and then by calling [[Flexmanager Sharedmanager] showexplorer]; You can 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.

iOS efficient debugging

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.