iOSアプリ開発でLLDBでView要素を操作する

色々なところに書いてあるが自分用のメモ

### LLDBからViewのアドレスを取得する

```
(lldb) expr -l swift -O -- import UIKit
(lldb) expr -l swift -O -- UIApplication.shared.keyWindow!.rootViewController!.view!.value(forKey: "recursiveDescription")!

<UILayoutContainerView: 0x7f9a3f41def0; frame = (0 0; 375 667); autoresize = W+H; gestureRecognizers = <NSArray: 0x6080004546d0>; layer = <CALayer: 0x600000036360>>
   | <UINavigationTransitionView: 0x7f9a3f41ec70; frame = (0 0; 375 667); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0x60000003bca0>>
   |    | <UIViewControllerWrapperView: 0x7f9a3f50b4a0; frame = (0 0; 375 667); autoresize = W+H; layer = <CALayer: 0x604000039520>>
   |    |    | <UIView: 0x7f9a3f70ce10; frame = (0 0; 375 667); autoresize = W+H; layer = <CALayer: 0x60c0000344e0>>
   |    |    |    | <UIScrollView: 0x7f9a40017000; frame = (0 64; 375 603); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x60c000241980>; layer = <CALayer: 0x60c0000367e0>; contentOffset: {0, 0}; contentSize: {375, 603}; adjustedContentInset: {0, 0, 0, 0}>
   |    |    |    |    | <UIView: 0x7f9a3f70d110; frame = (8 8; 359 587); autoresize = RM+BM; layer = <CALayer: 0x60c000037dc0>>
   |    |    |    |    |    | <UILabel: 0x7f9a43401770; frame = (30 30; 299 527); text = 'Many scientists opposed h...'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x604000099870>>
   |    |    |    |    | <UIImageView: 0x7f9a3f5135d0; frame = (369.5 593; 2.5 7); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x60400003cb40>>
   |    |    |    |    | <UIImageView: 0x7f9a3f50d1d0; frame = (365 597.5; 7 2.5); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = <CALayer: 0x60400003ca60>>
   |    |    |    | <_UILayoutGuide: 0x7f9a3f70e170; frame = (0 0; 0 64); hidden = YES; layer = <CALayer: 0x60c000037c60>>
   |    |    |    | <_UILayoutGuide: 0x7f9a3f70e370; frame = (0 667; 0 0); hidden = YES; layer = <CALayer: 0x60c000037c20>>
   | <UINavigationBar: 0x7f9a3f40ebf0; frame = (0 20; 375 44); opaque = NO; autoresize = W; layer = <CALayer: 0x6000000363e0>>
   |    | <_UIBarBackground: 0x7f9a3f40f600; frame = (0 -20; 375 64); userInteractionEnabled = NO; layer = <CALayer: 0x600000036040>>
   |    |    | <UIImageView: 0x7f9a3f4102f0; frame = (0 64; 375 0.5); userInteractionEnabled = NO; layer = <CALayer: 0x600000036440>>
   |    |    | <UIVisualEffectView: 0x7f9a3f410520; frame = (0 0; 375 64); layer = <CALayer: 0x600000036460>>
   |    |    |    | <_UIVisualEffectBackdropView: 0x7f9a3f601e40; frame = (0 0; 375 64); autoresize = W+H; userInteractionEnabled = NO; layer = <UICABackdropLayer: 0x608000423bc0>>
   |    |    |    | <_UIVisualEffectSubview: 0x7f9a3f603f20; frame = (0 0; 375 64); autoresize = W+H; userInteractionEnabled = NO; layer = <CALayer: 0x6080004248a0>>
   |    | <_UINavigationBarLargeTitleView: 0x7f9a3f40f860; frame = (0 0; 0 44); clipsToBounds = YES; alpha = 0; hidden = YES; layer = <CALayer: 0x600000036840>>
   |    |    | <UILabel: 0x7f9a3f5163e0; frame = (0 0; 0 0); userInteractionEnabled = NO; layer = <_UILabelLayer: 0x60400009a720>>
   |    | <_UINavigationBarContentView: 0x7f9a3f4111c0; frame = (0 0; 375 44); clipsToBounds = YES; layer = <CALayer: 0x6000000364c0>>
   |    |    | <_UIButtonBarButton: 0x7f9a3f429b60; frame = (0 0; 65 44); layer = <CALayer: 0x60000003b0e0>>
   |    |    |    | <_UIModernBarButton: 0x7f9a3f607260; frame = (8 11.5; 13 21); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x608000425080>>
   |    |    |    |    | <UIImageView: 0x7f9a3f70f060; frame = (0 0; 13 21); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x60c000037860>>
   |    |    |    | <_UIBackButtonContainerView: 0x7f9a3f619260; frame = (0 0; 65 44); autoresize = W+H; userInteractionEnabled = NO; layer = <CALayer: 0x6080004279c0>>
   |    |    |    |    | <_UIModernBarButton: 0x7f9a3f606f20; frame = (27 9; 38 23.5); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x608000033200>>
   |    |    |    |    |    | <UIButtonLabel: 0x7f9a3f613180; frame = (-1.5 3; 38 20.5); text = 'Back'; opaque = NO; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x608000092930>>
   |    | <_UINavigationBarModernPromptView: 0x7f9a3f418590; frame = (0 0; 0 44); alpha = 0; hidden = YES; layer = <CALayer: 0x600000037c60>>
   |    |    | <UILabel: 0x7f9a3f418bd0; frame = (0 25.5; 0 0); text = ''; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x600000097de0>>
```

```
   |    |    |    |    |    | <UILabel: 0x7f9a43401770; frame = (30 30; 299 527); text = 'Many scientists opposed h...'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x604000099870>>
```

↑からアドレスを取得できる

### View UI HierarckyからViewのアドレスを取得する

Print Descriptionからアドレスを取得できる

<img width="1613" alt="スクリーンショット 2018-07-24 16.47.31.png" src="https://qiita-image-store.s3.amazonaws.com/0/92771/dda9eee5-428b-5f34-a67f-c27f02aecd58.png">

## `unsafeBitCast`でViewオブジェクトを取得

オブジェクトを操作できるので、ここでView要素を操作できる

```
(lldb) expr -l swift -- unsafeBitCast(0x00007f9667c21660, to: UILabel.self).text = "Hello, world."
```

### 参考

- [ビルドで待たないための、Simulator 上で実行中の view をいじる方法](https://qiita.com/Kuniwak/items/67ec035b942155d70d73#view-%E3%81%AE%E4%B8%80%E8%A6%A7%E3%82%92%E7%A2%BA%E8%AA%8D%E3%81%99%E3%82%8B)