Xcode Debugging Tips

Source: Internet
Author: User

There is a lot of time spent on debugging in a programmer's daily development, and it's inevitable that you will need to use Xcode for IOS development. This blog introduces several ways in Xcode that can significantly improve the efficiency of code debugging.

"If debugging is the process of removing bugs, then programming must being the process of putting them in."
--edsger W. Dijkstra

Add condition

Sometimes we may create breakpoints in a loop, but it is obviously a very inefficient way to click Continue again and again until the conditions we want are present. Fortunately, Xcode provides us with conditional breakpoints.

First, insert a normal breakpoint in the following code

Right click on breakpoint, select Edit Breakpoint, enter i > in Condition column

This way, the breakpoint is triggered only when the program is running to meet the criteria.

Symbolic Breakpoint

Symbolic breakpoint is a very powerful breakpoint. Find Breakpoint Navigator in Xcode (you can use the shortcut command + 7) and click the plus sign at the bottom to see it.

After adding, enter Viewdidload in the Symbol field.
This will trigger a breakpoint when all viewdidload methods in the program are called.

Of course, we can also add breakpoints just for a particular class of methods. Enter [ClassName Viewdidload] (objective-c) or Classname.viewdidload (Swift) in the Symbol column.

Monitoring breakpoints

Most of our debugging program is to monitor the change of a variable, in the code where the variable appears to add a breakpoint is not only tired and may also miss, after the next one to delete, it is very tired.

We can do this simply by adding a monitoring breakpoint to the variable.

Locate the first occurrence of the variable, add a normal breakpoint, enter debug mode, right-click the variable in Variables View, and select the Watch variable name. In this way, each time the variable is changed, a breakpoint will be triggered to inform us.

We can see the changes in the Console. (Note: in Xcode 6.1.1, there seems to be some problem in monitoring the Swift variable, and the value of the variable cannot be displayed correctly)

Log Information Breakpoint

The most common Debug method should be NSLog and println. Usually we will print out various instance information in this way to detect the program running state.

However, this debugging method also has a very obvious flaw:

    1. Cannot add at run time

    2. Add too many to interfere with the line of sight and need to be removed or commented out in trouble

    3. will be compiled into the App and need to be closed in the official version (of course, we can use the macro to determine if it should be compiled, but this also requires extra action)

Fortunately, we have another option in Xcode.

Add a normal breakpoint in the following code, select Edit Breakpoint, then click Add Action, select Log Message, and enter the number in the input box : @number @.

Run the effect as shown

Because of the log output here, we can tick the bottom automatically continue after evaluating actions so that this breakpoint will only silently output the log for us.

Audible breakpoint

Same day log information breakpoint, edit normal breakpoint, Action select Sound. A set sound is emitted when a breakpoint is triggered. This Action, combined with automatically continue after evaluating actions option, can be a cool audible Bug. :)

Summarize

The above-mentioned log information breakpoints and the occurrence of breakpoints can be added trigger conditions. With these breakpoints, it is natural to be able to greatly improve the efficiency of debugging code in everyday development.

Resources

WWDC Debugging with Xcode

Intermediate breakpoints


Xcode Debugging Tips

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.