Swift 代码调试-善用XCode工具(UI调试,五种断点,预览UIImage...)

news/2024/7/1 2:33:41

原创Blog,转载请注明出处  
http://blog.csdn.net/hello_hwc?viewmode=list 
我的stackoverflow

profile for Leo on Stack Exchange, a network of free, community-driven Q&A sites


工欲善其事,必先利其器,强烈建议新手同学好好研究下XCode这个工具。比如Build Settings,Build Info Rules,Build Parse, Edit Scheme…


前言:这个Swift调试系列分为四篇

  1. 图形化界面调试
  2. LLDB常用命令
  3. LLDB进阶使用
  4. Zombie等其他调试

2015.12月和2016.1月主要更新iOS开发的设计模式和Instruments优化技巧,穿插着写一些别的。


断点

断点是调试中经常用到的,让代码停止在错误出现的地方,看看变量以及上下文实际的变化,往往就能够找到问题所在。

点击左侧部分就可以添加断点,再单机可以禁用单个断点

导航栏中的断点列表

可以右键来禁用,编辑,删除断点。


断点上下文

 
让我们来看看图中的四个区域

  1. 这里可以看到CPU,内存,磁盘以及网络情况。注意,只有在实际设备上才是有意义的
  2. 线程信息,可以看到当前停在main Queue上,app运行的时候也启动了其他几个队列。注意,调试的时候看看代码运行的线程是否正确很有必要
  3. 这里可以看到Local变量,Swift有个好处是按照Module来划分了变量,简单粗暴
  4. 这个区域是LLDB调试区域,可以用LLDB命令执行任何动态的代码

我们着重来看下3,4区域,新手往往只会看变量,其实这里有很多可以利用的信息 
 
其中 
A. 禁用,启用所有断点 
B. 继续执行 
C. 跳过这一行 
D. step in(例如进入到函数实现内部) 
E. step out (退出step in) 
F. 打开UI调试 (后问会详细阐述) 
G.模拟位置  
H.选择线程 
I. 查看线程调用堆栈 
其中I的截图如下 


条件断点

举个例子,我想停在第888次执行?总不能一次次的continue吧!

<code class="hljs perl has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"> <span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">for</span> var <span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">index</span> = <span class="hljs-number" style="margin: 0px; padding: 0px; color: rgb(0, 102, 102); box-sizing: border-box;">0</span>;<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">index</span> < <span class="hljs-number" style="margin: 0px; padding: 0px; color: rgb(0, 102, 102); box-sizing: border-box;">1000</span>;<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">index</span>++ {<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">print</span>(<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">index</span>)}</code><ul class="pre-numbering" style="margin: 0px; padding: 6px 0px 40px; box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">1</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">2</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">3</li></ul><ul class="pre-numbering" style="margin: 0px; padding: 6px 0px 40px; box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">1</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">2</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">3</li></ul>

右键断点,选择edit breakpoint 

然后填写条件

condition就是代码触发的条件 
ignore就是在断点触发前忽略几次 
action是断点触发后,执行的LLDB动作,这里很简单就是打印当前的sum 
options,执行完action后是否继续执行

可以看到运行的截图 


准备工作

接下来讲的几种断点添加方式都是,在断点导航底部,如图添加 


Swift Error 断点

这个在Swift开发中很常用

添加一个Swift Error断点

定义一个方法,来抛出Swift Error

<code class="hljs java has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">   func test() <span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">throws</span>{<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">throw</span> DemoError.Type1}<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">enum</span> DemoError: ErrorType {<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">case</span> Type1<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">case</span> TYpe2}</code><ul class="pre-numbering" style="margin: 0px; padding: 6px 0px 40px; box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">1</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">2</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">3</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">4</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">5</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">6</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">7</li></ul><ul class="pre-numbering" style="margin: 0px; padding: 6px 0px 40px; box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">1</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">2</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">3</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">4</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">5</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">6</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">7</li></ul>

然后,这样调用

<code class="hljs cs has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">do</span>{<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">try</span> test()
}<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">catch</span>{}</code><ul class="pre-numbering" style="margin: 0px; padding: 6px 0px 40px; box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">1</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">2</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">3</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">4</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">5</li></ul><ul class="pre-numbering" style="margin: 0px; padding: 6px 0px 40px; box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">1</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">2</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">3</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">4</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">5</li></ul>

会发现,在Swift Error发生的时候,断点触发

当然,Swift Error断点也支持编辑来捕获指定类型的Error

为了方便读者阅读,图片我没加水印,转发者请注明转自 Leo的CSDN博客(http://blog.csdn.net/hello_hwc?viewmode=list)


Exception断点

在抛出异常的时候触发 
这个在iOS开发中很常用 
有过iOS开发的都知道,Cocoa在错误的时候会抛出异常,而实用这个断点,会帮助我们捕获异常。 
例如 
随便performSelector,会抛出异常

<code class="hljs ruby has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">   <span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">self</span>.performSelector(<span class="hljs-string" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0); box-sizing: border-box;">"abcdefg"</span>)</code><ul class="pre-numbering" style="margin: 0px; padding: 6px 0px 40px; box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">1</li></ul><ul class="pre-numbering" style="margin: 0px; padding: 6px 0px 40px; box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">1</li></ul>

在添加了All Exception后,会停在这里 


Symbol断点

停在不方便直接加断点的地方 
例如,停在

<code class="hljs bash has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">-[NSObject <span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">set</span>Value:<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">for</span>UndefinedKey:]</code><ul class="pre-numbering" style="margin: 0px; padding: 6px 0px 40px; box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">1</li></ul><ul class="pre-numbering" style="margin: 0px; padding: 6px 0px 40px; box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">1</li></ul>

如图 
 
然后,这样调用

<code class="hljs bash has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">        self.setValue(<span class="hljs-string" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0); box-sizing: border-box;">"Dta"</span>, <span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">for</span>UndefinedKey: <span class="hljs-string" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0); box-sizing: border-box;">"dagd"</span>)
</code><ul class="pre-numbering" style="margin: 0px; padding: 6px 0px 40px; box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">1</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">2</li></ul><ul class="pre-numbering" style="margin: 0px; padding: 6px 0px 40px; box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">1</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">2</li></ul>

会发现断点触发 


Test Failure断点

这个就是使用XCTest框架来测试的时候,当Test Case的Assert失败的时候触发的断点。这里不截图了


变量图片预览

红圈中左边是预览,右边是打印Description


UI调试

如何打开UI调试 
这里写图片描述

图中的红圈部分,点击后,整个调试区域如图 
 
其中

  1. 用来查看View的层次结构,树状图
  2. View的可视区域,可以详细的看到View的叠加关系
  3. 选中某一个View后的属性

查看AutoLayout约束 
右键某一个View

然后选择Show Constraints,

注意,第一张图右上角的地址

<code class="hljs  has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">0x7f8169e7daa0</code><ul class="pre-numbering" style="margin: 0px; padding: 6px 0px 40px; box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">1</li></ul><ul class="pre-numbering" style="margin: 0px; padding: 6px 0px 40px; box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">1</li></ul>

这里先记着,这个地址对LLDB的调试很有用,下一篇我会讲到




http://lihuaxi.xjx100.cn/news/235633.html

相关文章

Linux上传和下载

下载&#xff1a;sz 上传&#xff1a;rz

iOSSharing #9 | 2019-05-19

目录 1. setNeedsLayout、layoutIfNeeded与layoutSubviews区别&#xff1f; 2. UIView与CALayer的区别&#xff1f; 3. loadView什么时候被调用&#xff1f;它有什么作用&#xff1f;默认实现是怎么样的&#xff1f; 4. UIViewController的完整生命周期&#xff1f; 5. UIView动…

Swift学习 OOP三大特性:继承、多态、封装

先看个例子 从上面的例子可以总结那么一句话:”学生是人”。也就是Student类继承People类。简而言之&#xff0c;学生是人&#xff0c;这句话是说得通的&#xff0c;但是”人是学生”这句话是说不通的&#xff0c;不是学生就不是人了嘛? 从代码中&#xff0c;我们可以看出S…

整理的一点MD5资料(第一部分)

MD5加密算法研究MD5简介&#xff1a; MD5的全称是Message-Digest Algorithm 5&#xff0c;在90年代初由MIT的计算机科学实验室和RSA Data Security Inc发明&#xff0c;经MD2、MD3和MD4发展而来。 Message-Digest泛指字节串(Message)的Hash变换&#xff0c;就是把一个任意长度的…

1001 害死人不偿命的(3n+1)猜想 (15 分)

题目链接 题目很简单验证给定的数需要经过几步才能到达1。 假设这个数n如果n是偶数&#xff0c;n n/2;如果n是奇数&#xff0c;n(3*n1)/2c代码 #include <iostream> using namespace std; int main() {int count 0;int n;cin >> n;while (n ! 1) {//是偶数if (n …

iOS学习笔记--01swift实现提示框第三方库:MBProgressHUD

本文使用swift语言使用MBProgressHUD。 开源项目MBProgressHUD可以实现多种形式的提示框。使用简单&#xff0c;方便。GitHud的下载地址是&#xff1a;https://github.com/jdg/MBProgressHUD/ 下载完成后&#xff0c;将MBProgressHUD.h和MBProgressHUD.m拖入已经新建好的Swift项…

Netty - ByteBuf索引管理

2019独角兽企业重金招聘Python工程师标准>>> 之前写过一篇文章介绍过JDK里面InputStream的mark(readlimit)和reset()方法&#xff0c;Java IO&#xff1a;使用mark/reset实现替换文件中字符串。相应的&#xff0c;可以通过调用markReaderIndex()/markWriterIndex()/…

Swift项目引入第三方库的方法

分类&#xff1a;iOS&#xff08;55&#xff09; 目录(?)[] Swift项目引入第三方库的方法 转自 http://blog.shiqichan.com/How-To-Import-3rd-Lib-Into-Swift-Project/ 以下&#xff0c;将创建一个Swift项目&#xff0c;然后引入3个库&#xff1a; Snappy 简化autolayout代码…