Swift-Objective-C加载类方法? [英] Swift - Objective-C load class method?

查看:92
本文介绍了Swift-Objective-C加载类方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Objective-C中,NSObject具有一个名为load的类方法,该方法在首次加载该类时被调用. Swift中的等效项是什么?

In Objective-C, NSObject had a class method called load that gets called when the class is loaded for the first time. What is the equivalent in Swift?

@implementation MyClass

+ (void)load
{
   [self registerClass];
}

@end

推荐答案

Swift 1.2之前的版本:

Prior to Swift 1.2:

override class func load() {
   NSLog("load");
}

从Swift 1.2开始,您不能再覆盖load方法.相反,查看方法initialize,它的行为与加载不同,它是在第一次在某处而不是在应用程序初始加载时引用该类时被调用的

As of Swift 1.2 you can no longer override the load method. Look into the method initialize instead, it behaves different than load though, it get's called the first time the class is being referenced somewhere rather than on application initial load

这篇关于Swift-Objective-C加载类方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆