なんとかするから、なんとかなる

エンジニア関係のことを書きます

iOS How to edit a View in debug mode by LLDB Console

Introduction

This article shows how to edit a view in debug mode by LLDB Console.

Preparation

First of all, Build an app on a simulator or device and open the [Debug View Hierarchy]

f:id:hopita:20180824213110p:plain

In this article, I'll show you how to change an information for UILabel.

Let's see a memory address at the view

After opening the [[Debug View Hierarchy], click the right button and select the [Print Description of UILabel] menu on the view you want to edit.

Then, on the lldb console shows the details of information for the view.

Printing description of $12:
<UILabel: 0x7fb65a6075a0; frame = (0 0; 250 20.3333); text = 'YOUR TEXT'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x6000000975c0>>

For the latter procedures, copy the memory address "0x7fb65a6075a0".

Let's see the information of the view by memory address

Let's see the information by the memory address as below.

Command

po ((UILabel *) 0x7fb65a6075a0)

Result

<UILabel: 0x7fb65a6075a0; frame = (0 0; 250 20.3333); text = ''YOUR TEXT'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x6000000975c0>>

In this time, you can see the same information as before you see.

Let's change the text string in the UILabel

By this command, you can edit the text string on the UILabel

e [[(UILabel *) 0x7fb65a6075a0) setText:@"newText"]

"e" is an alies of expression. It doesn't take care about which command you typed.

Let's reflection the changes immediately

By the procedure as you see before, the lldb console shows the string '$0 = "newText"'.

But on the simulator or device, it didn't reflect the changes yet.

If you press the continue button, it will be reflected soon.

In this time, let's reflect it immediately.

Try this command on the lldb console.

e (void)[CATransaction flush]

On the simulator or device, the changes will be reflection fine.