Swift 代码调试-善用XCode工具(UI调试,五种断点,预览UIImage...)
原创Blog,转载请注明出处
http://blog.csdn.net/hello_hwc?viewmode=list
我的stackoverflow
工欲善其事,必先利其器,强烈建议新手同学好好研究下XCode这个工具。比如Build Settings,Build Info Rules,Build Parse, Edit Scheme…
前言:这个Swift调试系列分为四篇
- 图形化界面调试
- LLDB常用命令
- LLDB进阶使用
- Zombie等其他调试
2015.12月和2016.1月主要更新iOS开发的设计模式和Instruments优化技巧,穿插着写一些别的。
断点
断点是调试中经常用到的,让代码停止在错误出现的地方,看看变量以及上下文实际的变化,往往就能够找到问题所在。
点击左侧部分就可以添加断点,再单机可以禁用单个断点
导航栏中的断点列表
可以右键来禁用,编辑,删除断点。
断点上下文
让我们来看看图中的四个区域
- 这里可以看到CPU,内存,磁盘以及网络情况。注意,只有在实际设备上才是有意义的
- 线程信息,可以看到当前停在main Queue上,app运行的时候也启动了其他几个队列。注意,调试的时候看看代码运行的线程是否正确很有必要
- 这里可以看到Local变量,Swift有个好处是按照Module来划分了变量,简单粗暴
- 这个区域是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调试
图中的红圈部分,点击后,整个调试区域如图
其中
- 用来查看View的层次结构,树状图
- View的可视区域,可以详细的看到View的叠加关系
- 选中某一个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的调试很有用,下一篇我会讲到
Swift 代码调试-善用XCode工具(UI调试,五种断点,预览UIImage...)相关推荐
- MacOS效率工具IQuickLook-多合一Quick Look快速预览与右键扩展信息工具
MacOS效率工具IQuickLook-多合一Quick Look快速预览与右键扩展信息工具 系统说明 Quick Look说明 什么是Quick Look Quick Look的局限 "I ...
- android ui 最新教程,Android更新UI的五种方式,androidui五种
Android更新UI的五种方式,androidui五种handler.post activity.runOnUiThread view.post handler+Thread AsyncTask 例 ...
- [Android开发]Android更新UI的五种方式
Android更新UI的五种方式: 1.handler.post 2.activity.runOnUiThread 3.view.post 4.handler+Thread 5.AsyncTask 下 ...
- 有哪些好用的视频剪辑工具?这五种编辑方法很好用
有哪些好用的视频剪辑软件呢?视频剪辑是一项常见的多媒体处理技术,它可以将多个视频片段合并为一个完整的视频,也可以对视频进行剪切.调整画面大小.增加特效等操作,以达到更好的视觉效果和传播效果.对于需要制 ...
- 苹果手机键盘html代码大全,CSS_CSS3制作苹果风格键盘特效,效果预览:html标签:- phpStudy...
CSS3制作苹果风格键盘特效 效果预览: html标签: 复制代码代码如下: CSS3 KeyBoard Q W E R T Y U I O P ? A S D F G H J K L return ...
- element ui 对话框改 模仿苹果手机 预览视图
修改了一下 element ui的对话框 做一个手机模拟预览的样式 <!-- 预览 --><div class="look_box"><el-dial ...
- 使用抖音开发工具真机调试Android可以,而ios不行,而预览可以
首先检查抖音版本是否太低咯, 其次是点击真机调试,点击切换至旧版真机调试即可
- pycharm调试时出现十分缓慢,变量数据没法预览的解决方法
file->settings->Build,Execution,Deployment->Python Debugger->Gevent compatible
- 厉害了,手把手教你搭建一个代码在线编辑预览工具
点击下方"前端开发博客",选择"设为星标" 回复"2"加入前端群 简介 大家好,我是一个闲着没事热衷于重复造轮子的不知名前端,今天给大家带来 ...
最新文章
- 图片基础与tf.keras介绍
- 利用对象的等待队列和锁队列管理线程。
- Windows Server 2008 配置IE使用代理
- wps怎么投递简历发到boss直聘_BOSS直聘情色招聘:洗脑传销广告漫天飞,还陷虚假招聘...
- Jzoj5237 最长公共子序列
- 微信「扫码支付」被诉侵犯专利权;苹果回应“iOS 13 频繁提醒 App 定位”;Python 2 退休 | 极客头条...
- echarts3使用总结2
- java权限管理与用户角色权限设计
- java redis令牌桶_Redis令牌桶算法在限速中的应用
- 播放最近的人人影视mp4视频出现了所有文字显示不正常的现象,及其解决方法
- 机械盘阵高并发——使用ImDisk 与 junction显著提高整体吞吐性能
- PID控制,matlab/simulink
- Oracle官网下载各版本JDK+查看各版本的API文档
- forward(转发)与redirect(重定向)的区别
- SMS发送流程 Android2.2
- ONNX Runtime: ubutnu16.04编译 (编到怀疑人生)
- HTML自动点名代码,js+html实现点名系统功能
- jOOQ-将两个表的连接提取到相应的POJO中
- VS 2019中使用qt
- Dapper - 微型 ORM 之王 (C#.NET)
热门文章
- Jenkins简介及在Windows上的简单使用示例
- C++11中std::shared_future的使用
- 图像配准----SIFT
- 如何给页面加上loding_如何给片头添加字幕?视频剪辑大神们都这样玩
- 网页学名为html文件,什么是HTML
- 周信东c语言实验二实验报告,周信东主编最新版C语言程序设计基础实验一实验报告.doc...
- Java项目:企业人事管理系统(java+SSM+jsp+mysql+maven)
- SpringBoot 操作elasticsearch
- Lambda表达式的运用(详尽版)
- H5打开预览PDF,PPT等文件