iOS main函数知多少
2017-05-28 本文已影响113人
码农二哥
iOS main函数
int main(int argc, char * argv[]) {
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
- 一个 iOS App 的 main 函数位于 main.m 中,这是我们熟知的程序入口。但对 objc 了解更多之后发现,程序在进入我们的 main 函数前已经执行了很多代码,比如熟知的 + load 方法等。
main函数之前都发生了什么
- 本节将跟随程序执行顺序,刨根问底,从 dyld 到 runtime ,看看 main 函数之前都发生了什么。
- 详细请看
- 简要
- static和全局变量什么时候初始化的呢?
- dyld递归加载动态链接库
- 其中libSystem加载时,由 libSystem_initializer 逐步调用到了_objc_init,这里就是 objc 和 runtime 的初始化入口
- libSystem其实类似个容器,包含了多个库,其中有libDispatch,而后者可能依赖libobjc(objc的runtime)。
- 猜一猜+load什么时候掉用的?
- 调用它的源代码可以在objc-runtime源码中看
- 类和分类是在什么时机搞的
- 其中libSystem加载时,由 libSystem_initializer 逐步调用到了_objc_init,这里就是 objc 和 runtime 的初始化入口
UIApplicationMain
UIKIT_EXTERN int UIApplicationMain(int argc, char *argv[], NSString * __nullable principalClassName, NSString * __nullable delegateClassName);
-
principalClassName
The name of the UIApplication class or subclass. If you specify nil, UIApplication is assumed. -
delegateClassName
The name of the class from which the application delegate is instantiated. If principalClassName designates a subclass of UIApplication, you may designate the subclass as the delegate; the subclass instance receives the application-delegate messages. Specify nil if you load the delegate object from your application’s main nib file. -
具备线程保活/接受事件的效果
objc-runtime加载时机.png
工具
-
otool
- otool(object file displaying tool) : 针对目标文件的展示工具,用来发现应用中使用到了哪些系统库,调用了其中哪些方法,使用了库中哪些对象及属性,它是Xcode自带的常用工具