background

For iOS developers, Xcode is our most common development tool. Being familiar with the common interface elements of Xcode tools, as well as the common shortcuts and debugging methods, is very important for efficient development and can often help us get more results with less effort. However, because these knowledge points are very trivial, so in the use of often forget, often need to check to check, this article specifically made a summary, hoping to help each iOS developer code such as flying!

Introduction to Xcode interface and common shortcut keys

1.1 Introduction to Xcode

The Xcode page is divided into the following five sections:

  • ToolBar Area: Mainly responsible for program debugging, editor function area show/hide
  • Editor Area: Code writing area
  • Navigator Area: Displays the list of project files
  • Debug area: used for program debugging, viewing object information, and printing logs
  • Utilities Area: Used to set object properties and add UI controls.

1.2 Resource Manager Shortcut Keys

The detailed functions are as follows:

  • Project Navigator: Add, delete, group, and manage files in a Project, view files, or edit their contents in the Edit pick area
  • Source Control Navigator: View Source Control working copies, branches, commits, tags, and remote code bases.
  • Symbol Navigator: Integrate all the symbols defined in the workspace. Basically, symbols are what the editor can recognize, eg: OC classes, structs, enumerated types, global variables, etc
  • Find Navigator: Finds any string, reference, definition, and invocation hierarchy in projects and frameworks.
  • Issue Navigator: View diagnostics, warnings, and error messages found during opening, analyzing, and building a project
  • Test Navigator: Creates, manages, runs, and reviews unit tests
  • Debug Navigator: Displays information about the CPU, memory, disk, and network of a project
  • Breakpoint Navigator: Add, delete, and edit breakpoints.
  • Report Navigator: View the history of build, run, and debug projects and source control tasks.

The corresponding shortcut keys are Command + 1 to 9

1.3 Display and hide some pages

From left to right are:

  1. Hide/display the project navigation page on the left
  2. Hide/display the project Debug area below
  3. Hide/display the right toolbar

The corresponding shortcut keys are as follows:

role Shortcut key combination
Hide/display the project navigation page on the left Command + 0
Hide/display the project Debug area below Command + Shift + Y
Hide/display the right toolbar Command + Option + 0

1.4 Common Shortcut Keys

1.4.1 System Screenshot Shortcut Keys

role Shortcut key combination
Capture the current entire screen to a file Command + Shift + 3
Captures the current screen to the clipboard Command + Shift + Control + 3
Intercepts the selected screen region to a file Command + Shift + 4
Intercepts the selected screen area to the clipboard Command + Shift + Control + 4

1.4.2 Shortcut keys for Xcode File Operation

role Shortcut key combination
Create a new file in the current project (usually including.h and.m files) Command + N
Creates a new project within the current project Command + Shift + N
Close the file currently displayed on the screen in the project Command + W
Minimize the Xcode compiler window Command + M

1.4.3 Xcode Debugging Shortcut keys

role Shortcut key combination
Compile and run Command + R
compile Command + B
Forced out of Command + Q
Stop running Command + .
Continue (to the next breakpoint, or to the end if not) Command  + Control +  Y
Step through F6
Jump into a function F7
Jump out of a function F8
Add/remove breakpoints Command + \ (the cursor must be on the specified line)
Static code analysis Command + Shift + B

In the new Mac, if the touch bar is matched, function keys such as F1 and F2 must be set first during single step debugging. Step: Open”System Preferences“, click”The keyboard“Option, in the”Touch bar displaySelect “from the” columnF1, F2 and other keys

If you do not perform the above Settings, you need to press the “FN” key in the lower left corner of the keyboard, and you can see that F1 keys are displayed in the touch bar.

1.4.4 Shortcut keys for editing in Xcode file

role Shortcut key combination
The indentation left Command + [
Right indent Command + ]
Current file search Command + F
Current file text replacement Command + Option + F
Global search shortcut key Command + Shift  + F
Global text substitution Command + Shift  + Option +  F
Search for the next place Command + G
Search the last place Command + Shift + G
Quickly jumps to the specified line of the current class Command + L
Quick lookup to open a class Command + Shift + O
Code collapses and opens Command + Option + ⬅️ or ➡️ the cursor must be placed in the method
Current file modifies local variable shortcuts Command + Control + E (must have cursor over a variable)
Formatting code Control + I
Locate the file where the code resides in the project navigator on the left Command + Shift  +  J
Quick switching between.h and.m files in the program Command + Control + ⬆️ or ⬇️
Go Back or go Forward Command + Control + ⬅️ or ➡️
Go to the beginning or end of this line of code Command + ⬅️ or ➡️
The current row moves up or down Command + Option + ] / [

2, Xcode common debugging methods

In general, the basic debugging approach for Xcode consists of the following parts: breakpoints, logging &&LLDB, performance detection, and view debugger.

2.1 the breakpoint

Breakpoints also have many types according to their functions and functions: normal breakpoints, conditional breakpoints, abnormal breakpoints, symbolic breakpoints and so on.

2.1.1 Common Breakpoint

The program pauses when it reaches a breakpoint. For example, if the breakpoint is hit at line 30, the program will stop at line 30 (note: the program has only reached the first 29 lines; line 30 has not actually been executed yet). . Just click next to the line of code to add a breakpoint, click again, and the breakpoint turns light blue to make it unavailable.

2.1.2 Conditional breakpoints

After the breakpoint is set, edit the breakpoint and set the corresponding filtering conditions. Right-click and the options box will pop up. The four options are as follows:

  • Edit BreakPoint: Edit BreakPoint.
  • Disable BreakPoint: Indicates that BreakPoint is invalid. Click on the breakpoint and the breakpoint becomes light blue.
  • Delete BreakPoint: Deletes a BreakPoint.
  • Reveal in BreakPoint Navigator: The BreakPoint tree on the left indicates the BreakPoint.

I’m mainly using the first one here: Edit BreakPoint. In this set breakpoint filter conditions (double-click breakpoints can also quickly enter the edit breakpoint dialog).

  1. Condition: Returns a Boolean value. When a Boolean value is true, a breakpoint is triggered.
  2. Ignore: Ignores the first N breakpoints and triggers the breakpoints after N+1 times.
  3. Action: events that trigger breakpoints, including the following six types:
    • AppleScript: Executes scripts.
    • Capture GPU Frame: Used for OpenGL ES debugging to Capture the current Frame drawn by the GPU at a breakpoint.
    • Debugger Command: the same as entering the LLDB debugging Command on the console.
    • Log Message: Outputs custom format information to the console.
    • Shell Command: receives Command files and parameter lists. Shell Command is executed asynchronously. If Wait until done is selected, the system waits for the Shell Command to be executed before debugging.
    • Sound: Plays Sound when the breakpoint is triggered.
  4. Options(Automatically continue after evaluating Actions) : When selected, the breakpoint will not terminate the program.

2.1.3 Abnormal Breakpoint

When using Xcode for software development, the total can not avoid abnormal and lead to program crash, but sometimes do not know where to go wrong, can only use the break point bit by bit test, although can also detect but always can not be in place in one step. Xcode actually has an exception breakpoint that can be set automatically on the line of code that causes the program to crash or throws an exception.

Step 1 one ️ :(1) open the Breakpoint navigator -> (2) click ➕ below -> (3) select “Exception Breakpoint”

Step 2 discount: Following the selection of the above steps, you will see the edit conditions of abnormal breakpoints, where you can edit your own abnormal breakpoint conditions according to your own needs:

The Exception option lets you select exceptions thrown in response to Objective-C objects as well as C++ objects. Break selects which exception the breakpoint receives, whether it receives an exception thrown by a “Throw” statement or a Catch statement.

Step 3 ️ : Run the program and solve the problem. Delete the breakpoint.

2.1.4 Symbolic breakpoints

Symbolic Breakpoint is a Symbolic Breakpoint that can be set for a method (function) and suspended. Sometimes, it is not clear when a function will be called, so we can use symbolic breakpoints to trace the program stack that called the function.

Step 1 One ️ : As setting abnormal BreakPoint, choose “Symbolic BreakPoint” after clicking ➕

Step 2 discount ️ : Edit the method name and conditions of the breakpoint.

  • Symbol: Fill in the method you want to set the breakpoint (for example: -[NSException raise], – for instance method, + for class method).
  • Module: specifies whether the method or function to set the breakpoint is in dylib. Default is not specified.
  • Conditon: Enter conditions such as: (BOOL)[item isEqualToString:@ “test”] before (BOOL) is required. Otherwise, the console displays a message indicating that the type does not match, causing the condition to fail to take effect. That means stop when item (NSString) is test.
  • Ignore: Ignores several times.
  • Action: Add additional actions (Applescript, capture animation frame rate, Debugger command (LLDB), enter log, terminal command (shell), play sound) after the program breakpoint is executed
    • Po item Prints the value of the item variable
    • Bt represents output method call stack information

Step 3 discount: If the method name is [UIView init], the new breakpoint method can be seen.

Step 4 discount: Run the project, at which point Xcode will stop in your breakpoint method.

2.2 Log Output

First might think about the log output, is in the code editor NSLog (), although at the time of print is very clear, but the downside is we need to add want to print on the position of NSLog and rerun the project code, so that will be waste of time, affect the development efficiency, and we are in the debugging process is used in the more break points, And then P or Po. The p and Po are print commands in LLDB. As shown in the image below, a breakpoint is made on line 29, and an “LLDB” dialog window appears on the right side of the lower console.

2.2.1 help command

Type help in LLDB and press Enter to see some common LLDB commands, as shown in the following figure. The common commands are Po, p, expression, call…

2.2.2 expression command

Expr or E: expression is the abbreviation of expr or E: expression. It can dynamically execute the specified expression during debugging and print the result. It is the most important command in LLDB debugging commands and the originator of p and Po commands that we often use later. The expression command provides two functions:

  • Execute expression
  • Output return value

Note: Although the expression command has the function of output return value, it is not commonly used in daily debugging. Generally, such printing function is replaced by p and Po commands. Expr is often used to modify the value of a variable during debugging.

2.2.3 p & print & e & call command

2.2.4 Po orders

All objects in the OC are represented by Pointers, and the object itself can be printed out as Pointers instead of objects. The object itself can be printed using -o. For convenience, the LLDB uses an alias Po for “expression -o –“, which can be used to output oc objects and object information.

2.2.5 call command

Method call: To call a method from a breakpoint and output the return value of the method.

2.2.6 image command

Common commands are as follows:

  • Image List: View the libraries used in the project
  • Image lookup: The image lookup command can be used to find the original address of the executable file or shared library. When the program crashes, you can use this command to find the specific location of the crash.

The following code:

Run this code and the crash message looks like this:

According to the above crash, which line of code has a problem is located according to the call stack information:

We can use the image lookup -a address or the image lookup –address address command in the LLDB to locate the location.

To view the call stack information step by step, we can see is in – [ContainerViewController viewDidLoad] method in ContainerViewController. M file line 36 collapse caused by an array.

2.3 LLDB Debugging dialog box

Detailed functions from left to right:

  1. Show/hide the console
  2. Breakpoint information: If it is blue, the breakpoint is valid. If you click on it and it turns gray, then all breakpoints don’t work.
  3. Continue: Clicking this button will resume the program from the current breakpoint until the next breakpoint
  4. Step over: Clicking this button will run step by step for easy debugging
  5. Step in: Clicking this button takes you inside the function
  6. Step out: Clicking this button will step out of the function and back into the function call, usually corresponding to the button on the left
  7. View debugger: View layers, detailed in Section 2.4 (p. 467)
  8. Menory Graph: Easy to view stack information
  9. Override environment variable Settings for the debugger
  10. Simulator location switch

2.4 Performance Check

Static analysis: Statically analyze code to find potential errors such as memory leaks, empty references, unused functions, etc. Method: Choose “Product” -> “Analyze” or use the Shift + Command + B shortcut key, and then find a way to destroy the arrow head.

2.5 View Debugger (Hierachy View)

Here are some view debuggers that are often used when viewing a UI:

Debug View Hierachy: Debug View Hierarchy. In addition to clicking the icon on the console, you can also choose Debug > View Debugging > Capture View Hierarchy from the menu to start View Debugging. View hierarchy can be viewed by clicking this button when a breakpoint is present or not.

With this layer relations, we can clearly know the view on the page of each control position relations, when we met in the development of testing a control does not display, control obscured, or need to look at the current view of the view class file names, such as when can view the debugger view open the debug view, improve the efficiency of our development.

2.6 Debug the navigator

The Debug Navigator is extremely useful when testing a project, where we can not only see the real-time status of the [CPU] processor, [Memory] Memory, [Disk] hard Disk and [Network] Network requests of the APP, but also view the call information of the current function when debugging the code.

2.6.1 Viewing Function Call Information

If a normal breakpoint is made at line 47 in the figure below, and the method execution stops at line 46, we can see in the debug navigator that the current function is called by which function on which thread. For example, in this case the lazy loading function of the viewModel is actually called in the -[ContainerViewController viewDidLoad] method of Thread 1 (Main Thread).

2.6.2 CPU Usage information

In order to develop a high-performance APP, we must pay attention to the following panel information and constantly improve it.

2.6.3 Memory Usage

2.6.4 Disk Usage Information

2.6.5 Network Request information

conclusion

This article mainly summarizes and introduces some basic operations about improving the efficiency of using Xcode, hoping to help iOS developers who are not familiar with this part of the knowledge to solve the problems they may encounter in actual study and work. If there is a mistake, welcome you to pass by the big guys criticism and correction, put forward valuable opinions. If you have any questions, I hope you can leave a message and make progress together. Thank you!

Hi, I’m Xiao Bo from Kuaishou E-commerce

Kuaishou e-commerce wireless technology team is recruiting talents 🎉🎉🎉! We are the core business line of the company, which is full of talents, opportunities and challenges. With the rapid development of the business, the team is also expanding rapidly. Welcome to join us and create world-class e-commerce products together

Hot job: Android/iOS Senior Developer, Android/iOS expert, Java Architect, Product Manager (e-commerce background), Test development… Plenty of HC waiting for you

Internal recommendation please send resume to >>> our email: [email protected] <<<, note my roster success rate is higher oh ~ 😘