XCODE Debug Mode Data Collation _ios

Source: Internet
Author: User
Tags explode mixed reset

In iOS development, most of the time you need to debug in debug mode, and here is the knowledge of debug

What is the difference between 1,debug and release versions?

As we all know, we have iOS development, in the Xcode debugger, divided into two ways, debug and release, in Target's setting believe you should see a lot of options are divided into debug and release, to facilitate our separate set, Meet the different requirements for debugging and publishing.

Release is a distribution version that has some optimizations than the debug version, with files smaller than debug files debug versions, debug and release calling two different underlying libraries. In layman's terms, we are using debug mode when we are working on our own internal real or emulator debugging, and when we want to publish, that is to say, when it is necessary for the public to use it, we need to build the release version, the difference is as follows:

One, debug is debug version, including program information more

Second, only the debug version of the program to set breakpoints, Single-step, using Trace/assert and other debugging output statements

Third, release does not contain any debugging information, so

Small size, fast running speed

Draw a knife to break a bug

Breakpoint, (I beg you not to think of Zhang Jingxuan, Ah Xuan he easy, small suffer again what?) You people are really!! ), I would say that the breakpoint is breakpoint! It's basically not crippled. The IDE has breakpoint debugging capabilities! Especially Xcode, our home IDE breakpoint debugging function but strong and strong in the hand! Before you get to know Hazi is a breakpoint? How does it come true? How's the working principle? Bo Shortcoming said his understanding of the bar, the breakpoint, as the name implies that there was a point, then it broke, thank you, my story finished. Alas, these basic knowledge of things themselves will not check? You really have the nerve to go to the sky, but also to Bo the Lord to you check yo, check yourself!

Normal operation

As shown in Figure 3

The basic breakpoint operations are as follows

Figure 4

Clicking on the Black column creates a breakpoint, click again to temporarily cancel this breakpoint (but not delete), long press that breakpoint dragged out on the delete (Mac OS system Engineer is the pleasure of porridge), of course, you can right-click the created breakpoint, will pop up the corresponding menu.

Of course, you can also monitor a variable!

Figure 5

In the object view, right-click on an object and click on "Watch ' xxx" to complete the monitoring of the XXX object.

Here i monitored the uilabel variable of lab, which is printed to the console whenever the variable updates its information.
All right! Our most basic task of creating breakpoints has been learned, Xcode comfortable in what place? is not in the debug mode and run mode, can be said to be seamless switch, as long as you do not create breakpoints, it is run normal mode, if the creation of breakpoints and run to the breakpoint, automatically into the debug mode, unlike an EC at the beginning of the IDE, the control panel is like flying aircraft, Tens of thousands of buttons think it's strong, in fact, only run and stop, and what debug mode, App mode ..., really xcode superiority in the contrast more intense, comfortable to the extreme, like the summer wind blowing chrysanthemum, ah yes is that kind of feeling!
We create a good breakpoint, and when we run to it, it stops automatically, like this:

Figure 6

The most basic operating skills of these debug are for every entry-level iOS developer to master, as a kind of instinct, like a dog who likes to eat Xiang (oh sorry bloggers are not discriminating against the dog, bloggers have also raised the dog, very two force but never eat Xiang!) Really, as far as I know, it never eats xiang, here is only metaphor.

Global Breakpoints (breakpoint)

Sometimes when a program is wrong, you can't pinpoint the line of code that runs, but go straight to the main loop or appdelegate, or you'll be prompted to:

EXEC_BAD_ACCESS:

Is there an urge to cry? NI ~ ~ Give me at least some stack information ... This time you must not hit the mouse and keyboard Oh, everything is the host in operation, you hit the mouse and keyboard what is the use? Should be kicking host AH ~ ~, now have a global breakpoint, the mother no longer worry you hit the mouse, you only need to:

Figure 7

In the debug navigation panel, you set up a global breakpoint, so whenever you encounter an error, the debug program will automatically locate the information on the bottom of the stack, which is the line of your first error code, so you can have a happy debug pull ~ ~

Conditional breakpoint (condational breakpoints)

There used to be a game called "masturbate," some players know how to operate, will put skills can walk, but they do not know to buy equipment, play a game down, shoes and knives have not bought. Why do I tell this story? Because a lot of kids learn things and play games, read the previous debugging skills, think that they have a dick explosion invincible, in fact, they are not equipped with the players, if only the use of the above debugging skills can only be said to be low play, in the tall force superior chicken is the kind of instantaneous seconds, So learn to equip yourself is the King! Conditional breakpoint, is to learn targeted!

Let's look at a piece of code

Figure 8

Do you want to ask the blogger why so coquettish, unexpectedly on Swift!! I just want to sing a wet: people laugh at me too lewd, my family lives in the yellow Crane Tower.
Anyway, this is a time when everyone is full of support, Bo is also the Lord, so to learn swift slightly.
If we use a breakpoint in a loop, if the loop executes 1 million times, your breakpoint will be executed so many times, don't you think the balls are cold? So we do this:

Figure 9

This allows breakpoints to be triggered only if the c== "H" is traversed.

Figure 10

Isn't it great?

Some children's shoes have a titanium alloy dog eye has seen the edit breakpoint there's an action thing, what's that?
This is very powerful, you can in the location of your breakpoint, perform a variety of operations, such as the execution of script commands, console commands (can make debugging information custom save), printing information, etc.,

Blogger favorite is this log message, simple and rough! There's no need for print at all, NSLog, it's just a matter of printing on the breakpoint (actually this is the high-energy product of the Xcode and the debugger, which is described below). It can be as follows:

Figure 11

In fact, just Bo Master Lied, Bo's favorite action is not log message, but sound, as the name suggests, breakpoint on the bug, so that the breakpoint will be issued a sound, hear my own set of voice, I know what is a bug, audible knowledge of bugs, hehe, exec_ Bad_access error I set up to Hatano teacher's voice, unrecognized selector send to INSTANCD error I set to become the teacher of Cang ... Don't ask me how the system does not Akiho Yoshizawa the sound of step, I do not know who is Akiho Yoshizawa step.

And, of course, there are more powerful conditions.

Figure 12

After adding, enter Viewdidload in the Symbol column.

As a result, breakpoints are triggered when all the Viewdidload methods in the program are invoked.

Figure 13


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

For example: Unrecognized selector sent to instance 0xaxxxx This error, this instance can be quickly positioned

Figure 14

The Art of printing

Although Arc has made memory management simple, time-saving, and efficient, it is still important to track some important events in the Life-cycles of object. After all, arc does not completely exclude the possibility of a memory leak or attempt to access an object being released. For this purpose, we can be very artistic to peeping at what the object is doing, think about the good pleasure.

NSLog

Small partners in the first class to learn the life cycle of Viewcontroller, the teacher must be very wretched to teach everyone, in Viewcontroller each life cycle method used NSLog to peep! Yes, this is actually the simplest way to track the life cycle of the explosion, but the system's own nslog really a bit weak, the output of too little information, simply can not meet our desire, here I teach you to strengthen your log!!
You can use the following macro

A better version of NSLog
#define NSLOG (format, ...) do {\
fprintf (stderr, "<%s:%d>%s\n", \ [[
NSS Tring stringwithutf8string:__file__] lastpathcomponent] utf8string],
__line__, __func__); \
(NSLOG) (format), # #__VA_ARGS__); \
fprintf (stderr, "-------\ n"); \
} while (0)

About the power of macros you can mess with my blog. Preliminary study on pre-compiling instructions in iOS

That's what it's like to print out. (In fact, NSLog's printing is very inefficient, even 100 times times lower than print, interested in turning over the Apple manual).
When using the OBJC language (strongly typed) and printing with NSLog, it is often unclear what type of output the NSLog (@ "%", XXX) XXX should be, or whether it is%d or%@ or%f??? Silly points not clear ~, so play nslog you should know the following several global methods!

Figure 17

Open Zombie Object (Enable Nszombie Objects)

Xcode can turn those that have been dropped to "zombies," and when we visit a zombie object, Xcode can tell us that the object being accessed is an object that should not exist. Because Xcode knows what this object is, it lets us know where the object is and when it happens.

So zombies is your good base friend! He can let you output the information more specific!!

To do this specifically:

Figure 15


Try to output the object again.

Zombies can only be used in simulators and OC language Oh ~

The aggressive code farmer

If you have the printing of the artistic use of the wind, and the use of breakpoints can be arbitrary, then you have in the confrontation with the forced superior chicken, the majority of the victory, you have been a Pride Leng's programmer, overlooking all the low energy of the forced superior chicken! But! Facing a more powerful enemy--your bald-1000° nearsighted teeth with leeks in front of the 10-year-old colleague, and Xiaolicangdao eyes with eyes that can kill your interviewer with eyes ... For them, you are still too weak, your skills are honed too little! So you have to advance!! More powerful enemy than the forced chicken! We need more powerful weapons.

Console (LLDB command)

Our goal is to arm the nose hair! Console window you know which one is black and a lot of words will roll out, especially when the chicken is forced to dry, then the students have not encountered this console it?

Figure 16


Our family's compiler history please mess with the pre-compile instructions in iOS, yes we are now using the world's best C, C + +, OC, Swift compiler--LLVM,LLDB is the world's best LLVM debugger! Don't be shy, because we are the best! So be sure to use the best! Do not be polite yo, casually use, just like their own home ah, ah haha eat food do not Just eat, many clip dishes ... Ah ~ Bo Master hospitable occupational disease and made a ~, what? You don't know where to use LLDB?

First of all! You'll have to crash or break the procedure! Until you see the word "lldb" in Figure 16, you can knock the order.

Every time you want to view variables, constants, you have to write nslog to print, and then recompile, to execute, start again? Too tired to have a lldb you just do it

Figure 18

Is it convenient to explode?

When you have a switch statement that you want to test every case, you have to make false conditions to test; there is a if...else ... statement, you want to test different situations, you have to hard-code write a different situation, compile several times in order to test each situation ..., I think you should know why your hair is so sparse.
These cases, just a compile, using the LLDB thread command, forged return value, cheat registers, you can do whatever you want to do all the tests.
Is it going to explode?

Lldb really strong, Bo master did not cheat you, this blog to this all debugging skills LLDB can be implemented, a variety of breakpoints, various printing, invoke Python plug-ins, run the interrupt, the operation of the bottom of the hardware, control program running threads ... lldb can do it! As if LLDB was another powerful world!!!
Is it strong to explode?

In fact, if you do not want to can chew, you just proficient in this debugging tool, the basic debugging skills you can not learn, where the blogger is also no, lldb strong is not a blogger casually say a few words can express the out,
More need everybody hands-on, can truly realize that kind of beautiful, that kind of carefree and incomparable debugging experience!
Here the blogger selflessly pulls out any door, here has very good article! Can let you have a good aftertaste, hehe

"The Lldb Debugger"
"About Lldb and Xcode"
A preliminary study of the LLDB Debugging command
Dances with the debugger-the waltz of Lldb

Profile (instruments)

Figure 19


How do you translate this thing? Let's call it an Inspector!! You may have studied iOS for half a year, and you've never noticed or used the tool, but the blogger is very responsible for telling you that any outstanding app on the market will use instruments to make the code more robust! is instrument an aphrodisiac? How can you make your code robust?

This strong is not that strong ~ ~ ~ ~ I'm only 18 years old can be a little more pure AH

Instrument contains a lot of tools, memory overflow analysis, performance analysis, various analysis ... If in detail, this really can open a blog for each tool, but the blogger is a person who knows how to teach people to fish as a way to teach the truth of the old driver! So bloggers certainly won't say it all again! Let's take a look at the use of the dedicated Debug memory overflow analysis tool!

Figure 20


You can try this "Analyze" before you use leaks.

Figure 21


Analyze can quickly discover problems with release in your code, and the lack of parent-class methods in the inheritance process! Generally a good iOS development engineer No Warning, pass analyze is the most basic conduct! I know that you have been numb to hundreds of warning of your own project, but I am the owner of the blogger to tell you that this is not good! , because there is a Yunnan folk song "The Old Driver takes me" to hear Bo main spirit refreshing!

Stick to the virginity of an iOS developer! Follow me and shout slogans!

No warning! Pass analyze!

We're going to go back and use leaks!. If analyze all pass, then can use leaks tool, discover thousand old demon level of side leak!

Figure 22


If you are prompted about the risk of a side leak, you can also eject the side extension details like this

Figure 23


Direct click Method can be directly into the code section!!

is not very simple rough it! Of course, a lot of other tools, but is called space is always limited people, ah real eggs pain ~ Really want to say a little more

Want to know more about instrument you can read this article!
Instruments in Xcode

Xcode View Debugging

Sometimes some of the best chickens hide deep, code almost all over again, or did not find the problem where, Bo Master can understand that the wind messy, eggs broken a hurrying feet, because countless day and night Bo Master is trapped in the middle of the can not extricate themselves, and then simply rebuild a new project directly! Or not!! I went, until one day the blogger woke up in the morning to see his handsome face in the mirror, I suddenly insight, the original handsome can be so quick to find bug! The final lock is lovely and hateful xib and storyboard out of the question!! Some constraint or view nesting logic, or team collaboration git conflicts, and so on, causing io-v error, this situation to check the view file, may Xcode crash can not open that xib or storyboard, You just use the Text tool to open this XML type of tag file, you almost spit blood, tens of thousands of lines of record dog eyes are blind ...

But this history is going to end!! Because of our powerful Xcode view debugging Function!!

The following content, complete copy, if there is discomfort, adhere to read! Please call me a happy Porter!
Copy from "View Debugging in Xcode 6"

Apple has made a number of significant improvements and optimizations in Xcode 6, and view debugging is one of them. Typically, the app user interface does not behave as expected by the developer, such as either not showing the view or not showing it correctly. This article explains how to use the new view debugging features of Xcode to simplify the developer's recognition and repair of the problem interface.

1.Demo Engineering

Start by downloading the sample project from GitHub (https://github.com/tutsplus/ViewDebugging) and opening the viewdebugging.xcodeproj. The project contains a simple clickable application, application delegate, and a storyboard that contains a few view controllers. The app is designed for the iphone, but benefits from the adaptive layout of iOS 8, so the interface shows no problem on any device.

The Application sample project you just downloaded is a simple to-do list application that contains a simple screen for other information, such as the number of items in the example project, the user avatar, and the @*** Twitter operation. Clicking the Run button in the upper-left corner of the Xcode will show the application running in the iOS emulator.

Figure 24


You will soon notice that there is a problem in the user interface-no data is displayed in the table view. Open Firstviewcontroller.swift in the Engineering navigation panel and locate the following code:

var mockNotesDataSource: [String] = ["Do some laundry", "Finish homework", "Walk the dog", "Learn about view debugging"]{didSet{self.tableView.reloadData()}}

You can see that the Mocknotesdatasource variable is the data source for the table view. With Swift's property watcher feature, the table view is automatically reloaded when the data source changes. By looking at the code snippet above, you'll see that there should be 4 items in the application that need to be shown, but now without the data it means there are errors in some places.

Enable View debugging

The problem seems to be related to the user interface. When running the app, press the Debug View Hierarchy button at the bottom, or select Debug > View debugging > Capture view hierarchy from the menu to start the Debug.

Figure 25


After you start the view debugging, Xcode will take a snapshot of the application's view hierarchy and display a three-dimensional prototype view to explore the level of the user interface. In addition to showing the view level of the app, this three-dimensional view shows the location, order, and view dimensions of each view, as well as how the views interact.

The example project shows normal in the three-dimensional view of the Xcode, but the table view cell seems a little too wide.

Figure 26


Pause application debugging and select Main.storyboard on the left to fix the problem. Click the table view and select Editor > Resolve Auto Layout Issues > Reset to suggested Constraints.

Figure 27


Compile and run the application again to make sure that the user interface shows up properly. Click the Debug View Hierarchy button to learn more about the features of view debugging.

View Debugging Features

Click and drag on either side of the three-dimensional rendering graph to rotate or skew the user interface, tilt left or right to select a table view.

When selected, Xcode highlights the view and displays the object and size checker on the right. View the top of the jump bar and confirm that UITableView is the last item on the right.

Figure 28

The Object and size checker includes a lot of useful information. In the past, developers needed to rely on log statements or breakpoints to check the configuration of a view.

Open the Size inspector (Spec checker) on the right, below the auto Layout, and you can see that the correct constraint has been applied to the view. In Object inspector, we can check the properties of the selected view.

Figure 29

There are 9 of buttons and sliders in the debug area of the Xcode that you want to use during debugging.
Figure 30


To sort from left to right controls:

Adjust the spacing between views: Adjust the spacing between different views.

Shows what is clipped: the part that is cut in the current display view.

Show constraints: Displays the constraints of the selected view.

Reset View Area: Restores the chart to the default state.

Adjust viewing mode: Selectively display a 3D rendering perspective, such as displaying only the content, presenting only the framework, and presenting both the content and the framework.

Zoom out: Reduce the 3D rendering perspective

Restore: Restores the rendering perspective to the default size.

Zooming in: Magnifying 3D rendering perspective

Adjust the visual view range: Hide or show views, step through the 3D rendering view, slide the slider to the left or right to the opposite effect.

It is recommended that you take a little time to operate these spaces and understand their usefulness.

View Layer Sorting

Compile and run the application again, and click the "More" tab at the bottom of the user interface. The first look at the interface looks OK, but it does not follow the developer's definition of accurate implementation, the image of the blur effect is not shown. We can better identify the problem by debugging the view hierarchy.

Drag the view to the left or right to see the situation, and then drag spacing slider to the right.

Figure 31


As a result, the spacing between the different views becomes larger, the level is also clearer, we see in the picture "below" also hides another view, select the hidden view, it is the "lost" visual effect view.
Figure 32


Open Main.storyboard and select Second View Controller Scene. In the document Overview panel on the left, expand the View object of the second view controller to view the sorting of the child views.

Xcode stacked views in ascending order in the document overview, in other words, the view at the top of the list is the basis of the view hierarchy.

Fixing the problem is simple. At run time, Blur Effect view is hidden under sky image because it is the first view in the view hierarchy. Click and drag Blur Effect View in the document overview, as shown in the following illustration:

Figure 33


Running the application again will see the blur effect. The user interface of the application looks the same as designed. We can also look at other debugging features of the iOS simulator to see what else is perfect.

5.iOS Simulator Debugging function

Compile and run the application, select the emulator, and choose the color blended layers option from the Debug menu.

Figure 34


You will then see that the app's user interface is covered in red and green, showing which layers can be overlaid and which layers are transparent. The mixed layer is a compute-intensive view, so it is recommended to use opaque layers as much as possible.
Figure 35


Apple noted this in its documentation (IOS Simulator User Guide) and used opaque layers on table view processing. There are some things that are not performing well when scrolling, and one important reason is that you use a mixed layer, and if the content background is opaque, the page scrolling effect is very smooth and smooth.

For this application, if the user has hundreds of projects to show, there may be inconsistent rolling performance. Table view cells are currently using a mixed layer. Because the view controller has a white background, the end user does not perceive any difference, regardless of whether the table view cell uses a mixed layer or an opaque layer.

Open Main.storyboard and select the Table View cell properties in the To Do list scene. In the property inspector (Attributes Inspector), scroll down the drawing partition and tick the opaque.

Figure 36


Compiles and runs the application with the color blended layers enabled. Because table view cells now use opaque layers, they are covered with green to indicate that they are opaque.

In addition to marking layers, there are other useful features to help developers debug applications in the iOS emulator. Here are some of the more useful:

Toggle slow animations in frontmost app: Check the emulator, turn on the Debug menu to select Toggle slow animations in Frontmost app, which can reduce the speed of animation in the app, suitable for debugging An application that contains complex animations. But also use the shortcut key command-t to operate.

Color Copied Images: This option adds a blue-green overlay layer to a picture that is copied by the core animation at drawing time.

Color misaligned Images: If the picture boundary is not perfectly aligned with the target pixel, this feature adds a layer of magenta to the picture. If the picture is drawn with a determined proportional size, the feature adds a layer of yellow overlay to the picture.

Color off Screen Rendered:. This option adds a yellow overlay layer to the off-screen rendering content.
Many developers ignore the design of the status bar when accessing the phone, and you can simply test by triggering the status bar on the call. In the iOS simulator, from

Toggle In-call Status Bar is selected in the Hardware menu.

To see how the app responds to events, press COMMAND-T to enable slow animations, and press Command-y to show the status bar when the phone is plugged in. If your application uses a navigation bar, the operating system will take care of it for you.

Figure 37


In addition to coloring the view, remember that the iOS emulator can also debug the core location problem. You can simulate a device in a specific latitude and longitude degree,

If your application uses icloud to manage data, you can also manually trigger synchronization events.

The demo app used in this article is very simple, and the techniques mentioned in this paper will save you a lot of time in the future. View debugging can help you correct many of the problems that appear in the user interface.

In addition to Xcode and Interfacebuilder, debugging with the iOS emulator can improve application performance and identify bottlenecks in the development process. Apple's human-Computer Interaction Guide (English version in Chinese) emphasizes the importance of active response to the app, making it easier to use and manipulate the application. Apple's promotion of interfacebuilder makes view debugging as simple as ever.

Conclusion

This article was spent 3 weeks by the blogger, on and off, there should be a lot of mistakes, but in any case I think it should be worthy of the title of the luxury package, which iOS development of the basic, common and high level of debugging skills are involved, but there are still many other odd-door skillful technology has not been introduced to, The main is hateful "space" limit Bo master Broad love, but anyway, this article can be considered as a debugging technology directory, because the blogger wrote here is very superficial.

Thank you for reading, I hope to help you, thank you for your support for this site!

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.