美文网首页
ios load 与 intalized

ios load 与 intalized

作者: 俊俊吖 | 来源:发表于2017-07-24 15:40 被阅读0次

    一.load 的调用

    load 方法会在加载类的时候就被调用,也就是 ios 应用启动的时候,就会加载所有的类,就会调用每个类的 + load 方法。
    2.load 方法的调用顺序 父母优先于子类 子类优先于分类
    举个例子:首先创建两个类 其中一个继承与另一个

    import "Person.h"

    @implementation Person
    +(void)load{
    NSLog(@"%s",func);
    }
    +(void)initialize{
    NSLog(@"%s %@",func,[self class]);
    }
    -(instancetype)init{
    if (self = [super init]) {
    NSLog(@"%s",func);
    }
    return self;
    }

    //在创建一个类继承与Person

    import "Girl.h"

    @implementation Girl
    +(void)load{
    NSLog(@"%s",func);
    }
    +(void)initialize{
    [super initialize];
    NSLog(@"%s %@",func,[self class]);
    }
    -(instancetype)init{
    if (self = [super init]) {
    NSLog(@"%s",func);
    }
    return self;
    }
    @end

    再创建一个Person的分类

    import "Person+Run.h"

    @implementation Person (Run)
    +(void)load{
    NSLog(@"%s",func);
    }
    @end

    int main(int argc, char * argv[]) {
    NSLog(@"%s",func);
    @autoreleasepool {
    return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
    }
    运行一下结果如下
    2017-07-24 10:53:37.746 RunTime[1294:36243] +[Person load]
    2017-07-24 10:53:37.748 RunTime[1294:36243] +[Girl load]
    2017-07-24 10:53:37.749 RunTime[1294:36243] +[Person(Run) load]
    2017-07-24 10:53:37.749 RunTime[1294:36243] main

    //load 方法一定是在main之前调用的

    二.initalized(不需要初始化也调用)
    相当于一个懒加载的方式,在init之前调用 而且之后调用一次,如果子类有initalized方法,先调用父类的initalzied 在调用子类的 调用完之后才会调用init的方法,init的方法也是先调用父类的,再调用子类的。

    举个例子:
    我在添加一个继承与Person的类Son里面不写任何方法

    import "Son.h"

    @implementation Son

    @end
    在ViewController里面调用才会启动 initalzied 和 init方法

    @interface ViewController ()

    @end

    @implementation ViewController

    • (void)viewDidLoad {
      [super viewDidLoad];
      Person *a = [[Person alloc]init];
      Person *b = [[Person alloc]init];
      Son *c = [Son new];
      Girl *d = [[Girl alloc]init];

      // Do any additional setup after loading the view, typically from a nib.
      }
      调用的结果:
      2017-07-24 15:21:16.006 RunTime[4078:106505] +[Person load]
      2017-07-24 15:21:16.007 RunTime[4078:106505] +[Person initialize] Person
      2017-07-24 15:21:16.007 RunTime[4078:106505] +[Person initialize] Girl
      2017-07-24 15:21:16.008 RunTime[4078:106505] +[Girl initialize] Girl
      2017-07-24 15:21:16.008 RunTime[4078:106505] +[Girl load] Girl
      2017-07-24 15:21:16.008 RunTime[4078:106505] +[Person(Run) load]
      2017-07-24 15:21:16.008 RunTime[4078:106505] main
      2017-07-24 15:21:16.233 RunTime[4078:106505] -[Person init]
      2017-07-24 15:21:16.234 RunTime[4078:106505] -[Person init]
      2017-07-24 15:21:16.234 RunTime[4078:106505] -[Person init]
      2017-07-24 15:21:16.234 RunTime[4078:106505] -[Girl init]
      2017-07-24 15:21:16.234 RunTime[4078:106505] -[Person init]
      2017-07-24 15:21:16.235 RunTime[4078:106505] -[Girl init]
      因此可以总结出:initalized只调用一次,子类先调用父类的

    相关文章

      网友评论

          本文标题:ios load 与 intalized

          本文链接:https://www.haomeiwen.com/subject/tfzvkxtx.html