iOS调试技巧

在iOS开发过程中,难免会遇到一些bug,那么遇到bug该怎么办?如何找bug呢?

对于bug,我们要合理的假设,找原因;对原因与bug之间的因果关系进行分析。

lldb的使用

主要是打断点的时候使用lldb.

查看值

1>非对象的值
int a = 10;

(lldb) p a
(原始命令:p = print = expression –)
输出:(int)$0 = 10
(lldb)p 10 + 10
输出:$1 = 20

2>对象的值
NSString *string = @“Hello,world”;

(lldb) po string
(原始命令:po = expression -o – = e -o –string)
输出:Hello,world

注意:清空控制台:command+K

修改变量的值

1
2
3
4
5
6
if(a==10){
NSLog(@"第一个if被执行了");
}
if(a!=10){
NSLog(@"第二个if被执行了");
}

(lldb) e(代表expression表达式) a=11
输出:(int) $0 = 11

定义一个变量

(lldb) e int $b = 20
(lldb) e int $b + $0
输出:(int) $1 = 31

调用一个函数

代码中:

1
2
3
4
5
[self test];
}
- (void)test{
NSLog(@"test被调用了");
}

(lldb) call [self init]
输出:test被调用了

函数提前返回,返回一个特定值

代码实现:

1
2
3
4
5
6
7
  NSInteger returnValue = [self testReturn];
}
- (NSInteger)testReturn{
int a = 0;
int b = 10;
return a+b;
}

(lldb) thread return 100
(lldb) p returnValue
输出:(NSInteger) $1 = 0
(NSInteger) $2 = 100

流程控制

1>继续运行 (相当于控制台第一个按钮)
c = continue = process continue
2>step over
n = next = thread step-over
3>step in(跳入函数)
s = thread step-in
4>step out(跳出函数)
finish = thread step-over

文章目录
  1. 1. lldb的使用
    1. 1.1. 查看值
    2. 1.2. 修改变量的值
    3. 1.3. 定义一个变量
    4. 1.4. 调用一个函数
    5. 1.5. 函数提前返回,返回一个特定值
    6. 1.6. 流程控制
本站总访问量 本站访客数人次 ,