Files
knowledge-kit/Chapter1 - iOS/1.59.md
2020-02-25 17:46:51 +08:00

21 lines
1.0 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 零散知识
1. 为什么线上代码尽量不要使用 NSLog("%@", person)?
因为NSLog使用%@输出本质上是调用了对象的 description 方法,所以代码中存在大量的 NSLog 的时候。会造成性能问题。
解决方案使用宏定义判断当前代码的运行环境DEBUG 模式下才输出 NSLog否则就空实现。
```objective-c
#ifdef DEBUG
///一个区分开发和线上环境的Log。NSLog的本质是调用对象方法的 description 方法所以线上代码使用NSLog会造成性能和安全问题
#define SafeLog(...) NSLog(__VA_ARGS__)
#else
#define SafeLog(...)
#endif
```
2. 如果我们想在某个函数或者方法参数指定参数的类型的话,使用 `id` 编译器不会在编译阶段对其真正的类型做检查,如果我们想指定为一个类的对象或者一个类的子类对象的时候可以使用 `__kindof` 。
```Objective-c
- (void)test:(__kindof UIView *)view
{
view.subviews;
}
```