是否从init方法开始?

发布于 2024-11-25 02:18:40 字数 120 浏览 1 评论 0原文

我想知道我们什么时候在项目中使用“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

掀纱窥君容 2024-12-02 02:18:40

init 方法用于在分配对象后对其进行初始化。
如果未定义,则调用基类实现,就像 Objective-C 中的任何其他方法一样,如果基类没有 init 方法,则调用基类的基类' init 将被调用,依此类推,直到 NSObjectinit 被肯定提供。

如果您没有声明 init 方法,那么您将无法正确初始化类中的 ivars(如果有)。

关于applicationDidFinishLaunching,这是一个非常特殊的类的方法,任何 Cocoa/iOS 应用程序都拥有它的应用程序委托。例如:(

 @interface myAppDelegate : NSObject <UIApplicationDelegate> {
 ...
 }

注意:UIApplicationDelegate 也可以是 NSApplicationDelegate

如您所见,此类派生自 NSObject,因此,框架将通过调用 <该类中定义的 code>init 方法(具体取决于框架)。然后,在某个时刻,框架将调用 applicationDidFinishLaunching 方法,并且您可以进行更改以初始化所有委托的成员并执行启动应用程序所需的任何操作。

在这种情况下,您不需要在 init 方法中进行初始化,而是在 applicationDidFinishLaunching 中进行初始化,这一事实与 UIApplication(或 NSApplication)之间的特定交互模式有关。对象及其委托。苹果是这样定义这个模式的;

  1. 您可以在 MainWindow.xib 中指定您的应用程序委托类,或者在 main.c 中调用 UIApplicationMain

  2. 框架负责实例化这些类(以程序员不需要处理的自定义方式)并调用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 the NSObject's init 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:

 @interface myAppDelegate : NSObject <UIApplicationDelegate> {
 ...
 }

(N.B.: UIApplicationDelegate could be as well NSApplicationDelegate)

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 the applicationDidFinishLaunching 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 in applicationDidFinishLaunching 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;

  1. you specify your application delegate class either in your MainWindow.xib or when calling UIApplicationMain in main.c;

  2. 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.

篱下浅笙歌 2024-12-02 02:18:40

除非您想要进行一些自定义初始化,否则没有必要实现 init 方法。

Doc 说,

子类应该重写 init 方法以添加特定于类的初始化代码。 init 的子类版本需要通过向 super:

发送消息来合并它们继承的类的初始化代码

Implementing init method is not necessary unless you want to do some custom initialization.

Doc says,

Subclasses should override the init method to add class-specific initialization code. Subclass versions of init need to incorporate the initialization code for the classes they inherit from, through a message to super:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文