• Xcode and LLDB Advanced Debugging Tutorial: Part 2
  • Fady Derias
  • The Nuggets translation Project
  • Permanent link to this article: github.com/xitu/gold-m…
  • Translator: kirinzer
  • Proofread by: iWeslie, JasonWu1111

In the first part of the three-part tutorial, we showed you how to manipulate existing attribute values using Xcode breakpoints and inject new code using expression statements.

I specifically developed a demo project with several errors that detailed how to use different types of breakpoints in conjunction with LLDB to fix errors in a project/application.

Before continuing, it’s a good idea to look at part 1 of this tutorial.

Let me remind you that the important rule of this tutorial is that you don’t have to stop the compiler or re-run the application after you run it for the first time, you will fix these errors at run time.

Observation points 👀

Let’s move on to the next enemy.

  1. Users can load articles more than 7 times.

Here are the steps to duplicate the error:

Turn on the Internet connection for your phone or emulator.

Scroll to the bottom of the show table view to load more data.

Scroll to load more articles more than 7 times. (Remember, for the current application, the user can only load the article seven times.)

One way to consider this error is to figure out how the pageNumber integer property has changed since it was passed into the network manager to retrieve the new article object with the specified pageNumber. You’ll spend some time and effort in a code base that you don’t yet understand and figuring out where the error occurred.

Don’t worry! Now let’s do something magical 🎩

In the first part of this tutorial, you learned that the GET HTTP request takes place with the pragma Mark Networking tag. There’s only one method, loadPosts. Place a breakpoint on the first line of the method, and then pull down the refresh to load the new article object. This action triggers the breakpoint you just set.

At the bottom of the debugger window, click the Show Variable View button. And then I’m going to slide out a view that contains all the properties of the PostsTableViewController.

Locate the pageNumber property, right-click, and choose “Watch _pageNumber”/” Watch pageNumber “.

This creates a breakpoint called “watch point” for the pageNumber property. A watch point is a breakpoint that pauses the debugger when the next watch property changes.

Continue with the program. The debugger will pause and you should see something like the following:

Objective-C

  1. A log of the old and new values of the pageNumber property.

  2. The method call stack that causes the pageNumber property to change.

  3. The current point that causes the pageNumber property to actually change. This is the setter method for the property.

If you go back to the first point on the method call stack, it will lead you to the following code:

Swift

  1. The debugger console informs you that the set watch point is triggered.

  2. The method call stack that causes the pageNumber property to change.

  3. The current point that causes the pageNumber property to actually change. This is a method called updateForNetworkCallEnd.

Obviously, whenever an HTTP GET request succeeds, the pageNumber property is incremented by one whenever the state enumeration property is active. The state enumerated property can be “Active” or “Inactive”. The “active” state means that the user can continue to load more articles (that is, the upper limit number is not reached). The “inactive” state is the opposite. In conclusion, we need to implement some logic inside updateForNetworkCallEnd to check the pageNumber property and set the corresponding State enumeration property.

As you learned earlier, the best way to do this is to test the hypothesis before actually modifying the code without stopping the compiler.

You guessed it 😉

Importantly, there is already a well-implemented method under Pragma Mark Support to set the state enumeration property. This method is setToInactiveState.

Add a breakpoint on the conditional statement line. Then add a debugger action and fill in the debugger command below.

Objective-C

expression if (self.pageNumber >= 7) {[self setToInactiveState]}
Copy the code

Swift

expression if (self.pageNumber >= 7) {setToInactiveState()}
Copy the code

Once you have done this, you need to deactivate the breakpoint that you set for the watch point and deactivate the watch point.

Now go back to the top of the table view, pull down to refresh, and then scroll down.

Don’t get too excited, we still have a big problem to solve 😄⚔️

Where to next?

See Part 3 for a tutorial that fixes the last error and learns about a new breakpoint type, symbolic breakpoints.

If you find any mistakes in your translation or other areas that need to be improved, you are welcome to the Nuggets Translation Program to revise and PR your translation, and you can also get the corresponding reward points. The permanent link to this article at the beginning of this article is the MarkDown link to this article on GitHub.


The Nuggets Translation Project is a community that translates quality Internet technical articles from English sharing articles on nuggets. The content covers Android, iOS, front-end, back-end, blockchain, products, design, artificial intelligence and other fields. If you want to see more high-quality translation, please continue to pay attention to the Translation plan of Digging Gold, the official Weibo, Zhihu column.