是否从init方法开始?
我想知道我们什么时候在项目中使用“init”方法?我见过很多没有它的项目,他们使用“applicationDidFinishLaunching”来启动应用程序,但不是 init 方法,你对此有什么想法吗?
多谢
i was wondering when we use the "init" method in a project? i have seen many projects without it, they used "applicationDidFinishLaunching" to start the application, but not the init method, do you have any idea about that?
Thanks a lot
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
init
方法用于在分配对象后对其进行初始化。如果未定义,则调用基类实现,就像 Objective-C 中的任何其他方法一样,如果基类没有
init
方法,则调用基类的基类'init
将被调用,依此类推,直到NSObject
的init
被肯定提供。如果您没有声明
init
方法,那么您将无法正确初始化类中的 ivars(如果有)。关于
applicationDidFinishLaunching
,这是一个非常特殊的类的方法,任何 Cocoa/iOS 应用程序都拥有它的应用程序委托。例如:(注意:
UIApplicationDelegate
也可以是NSApplicationDelegate
)如您所见,此类派生自 NSObject,因此,框架将通过调用 <该类中定义的 code>init 方法(具体取决于框架)。然后,在某个时刻,框架将调用
applicationDidFinishLaunching
方法,并且您可以进行更改以初始化所有委托的成员并执行启动应用程序所需的任何操作。在这种情况下,您不需要在
init
方法中进行初始化,而是在applicationDidFinishLaunching
中进行初始化,这一事实与 UIApplication(或 NSApplication)之间的特定交互模式有关。对象及其委托。苹果是这样定义这个模式的;您可以在 MainWindow.xib 中指定您的应用程序委托类,或者在
main.c
中调用UIApplicationMain
时框架负责实例化这些类(以程序员不需要处理的自定义方式)并调用
applicationDidFinishLaunching
,以便您可以使用它执行任何您需要的操作。我希望这个解释可以帮助您理解应用程序委托发生了什么以及为什么不声明
init
方法。The
init
method is used to initialize an object after allocating it.In case it is not defined, then the base class implementation is called, as with any other method in Objective-C, and if the base class has got no
init
method, then the base class' base class'init
will be called, and so on, up to theNSObject
'sinit
which is for sure provided.If you don't declare an
init
method, then you will not be able to properly initialize the ivars in your class, if any.About
applicationDidFinishLaunching
, this is a method for a very special class that any Cocoa/iOS application has got, its application delegate. For example:(N.B.:
UIApplicationDelegate
could be as wellNSApplicationDelegate
)As you see, this class derives from NSObject, so, it will be inited by the framework by calling an
init
method defined in that class (which one specifically depends on the framework). Then, at some point, the framework will call theapplicationDidFinishLaunching
method and you have a change to initialize all your delegates' members and do whatever you need to start your app up.In this case, the fact that you are not required to do initialization in an
init
method but inapplicationDidFinishLaunching
is related to a specific pattern of interaction between your UIApplication (or NSApplication) object and its delegate. This pattern was defined by Apple in this way;you specify your application delegate class either in your MainWindow.xib or when calling
UIApplicationMain
inmain.c
;the framework is responsible to instantiate those class (in a custom way that programmers do not need to deal with) and call
applicationDidFinishLaunching
so that you can do whatever you need with it.I hope this explanation helps you understanding what is going on with the app delegate and why you do not declare an
init
method.除非您想要进行一些自定义初始化,否则没有必要实现 init 方法。
Doc 说,
Implementing init method is not necessary unless you want to do some custom initialization.
Doc says,