【iOS】description 方法 和 SEL

版权声明:本文为博主原创,如需转载请注明出处。

description

-description 方法

使用 NSLog%@ 输出某个对象时,会调用对象的 -description 方法,并拿到返回值进行输出

+description 方法

使用 NSLog%@ 输出某个对象时,会调用类对象 +description 方法,并拿到返回值进行输出

1
2
3
Class c = [Person class];

NSLog(@"%@", c);
  1. 会调用类的 +description 方法
  2. 拿到 +description 方法的返回值(NSString *)显示到屏幕上
  3. +description 方法默认返回的是“类名”

默认输出

默认情况下,利用 NSLog 和 %@ 输出对象时,结果是:<类名:内存地址>

1
2
3
4
Person *p = [[Person alloc] init];
p.age = 20;
p.name = @"Jack";
NSLog(@"%@", p);
  1. 会调用对象p的 -description 方法
  2. 拿到 -description 方法的返回值(NSString *)显示到屏幕上
  3. -description 方法默认返回的是“类名+内存地址”

修改NSLog 的默认输出

重写 -description+description 方法即可

死循环陷阱

  • 如果在 -description 方法中使用 NSLog 打印 self

输出注意

1
2
3
4
5
6
7
8
Person *p = [[Person alloc] init];

// 指针变量的地址
NSLog(@"%p", &p);
// 对象的地址
NSLog(@"%p", p);
// <类名:对象地址>
NSLog(@"%@", p);

给一下官方文档的说明截图

![](http://ww2.sinaimg.cn/large/a9c4d5f6gw1f6s6z6ry28j21ic212qgc.jpg)

SEL

方法的存储位置

  • 每个类的方法列表都存储在类对象中
  • 每个方法都有一个与之对应的 SEL 类型的对象
  • 根据一个 SEL 对象就可以找到方法的地址,进而调用方法
  • SEL 类型的定义
1
typedef struct objc_selector *SEL;

SEL 对象的创建

1
2
SEL s = @selector(test);
SEL s2 = NSSelectorFromString(@"test");

SEL 对象的其他用法

1
2
3
4
5
6
// 将 SEL 对象转化为 NSString 对象
NSString *str = NSStringFromSelector(@selector(test));

Person *p = [Person new];
// 调用对象p的test方法
[p performSelector:@selector(test)];

SEL其实是对方法的一种包装,将方法包装成一个SEL类型的数据,去找对应的方法地址。找到方法地址就可以调用方法

其实消息就是SEL

1
2
3
4
5
6
7
8
9
10
11
12
13
- (void)test2
{
// _cmd代表着当前方法

NSString *str = NSStringFromSelector(_cmd);



// 会引发死循环
// [self performSelector:_cmd];

NSLog(@"调用了test2方法-----%@", str);
}

新博客文章地址:description 方法 和 SEL
CSDN文章地址:description 方法 和 SEL