In the iOS development and debugging process, every time after changing the code, we need to run again to see the effect, so it is very troublesome and time-consuming. The development time is already tight, so we have to do everything possible to save time and improve the development efficiency.
I used some advanced debugging in OC project before, and there were many articles on the Internet. I also tried many times and found a lot of materials in SWIFT project, so I made a special record. Therefore, today I will only talk about the operation closely related to SWIFT, which is also very simple, and as for other advanced debugging skills of LLDB, We can search on the Internet by ourselves, we will make records later, but also please pay more attention to 😄.
Scenarios and code
There is a label on the view, and the initial background color is red. During debugging, I need to change the background color to blue, but I don’t want to run the project again. This is just simulating a scene. Change the color to make sure it’s the view you’re looking for. The code is shown in the following figure, which is relatively simple and directly put a figure. So we have a break point in ‘touchesBegan’.
Debugging process
1. Check thelabel
Memory address and record
-
Click view Debug button, now the background color is red, find the memory address of label as shown in the picture below and record 0x104C124A0, which will be used later.
2. Trigger the breakpoint to change the background color
-
Release the View Debug mode from the previous step, tap the screen, trigger the breakpoint, and then enter the commands shown in the following figure in sequence on the console.
expression let $myLb = unsafeBitCast(0x104c124a0, to:UILabel.self)
Custom variables getlabel
.expression $myLb.backgroundColor = .blue
Change the color to blue- Let go of the breakpoint
(lldb) expression let $myLb = unsafeBitCast(0x104c124a0, to:UILabel.self) (lldb) expression $myLb.backgroundColor = .blue () $R1 = {} (lldb) c Copy the code
Note: the $character in front of $myLb must have the LLDB custom variable rule.
3. View the modification result
- I use directly
view debug
Button to show you the results of the implementation, no phone screenshots.