The relationship between AppDelegate and main.m

Well, I'm completely new to obj-c + cocoa, so this is probably obvious, but here goes:

I switched from command line applications to cocoa applications, learning how to work with objective-c in Xcode. One thing that I really don't understand is the role of AppDelegate and how it connects to main.m

It seems you could put your whole program in appdelegate, and it would work fine, and you don’t even need main.m, but not vice versa, if you are creating a cocoa application you should at least have appdelegate.

I have done a lot of web development and php command line tools, so I assume that I am looking for a file that the program will execute first and is designed to “manage” the rest.

Can someone help me understand what is happening in the cocoa program, like AppDelegate and main.m (or not related), and what should be the program flow?

+7
objective-c cocoa delegates
source share
2 answers

A key feature of many object-oriented systems (for example, Cocoa) is the "control inversion" , which basically means that the infrastructure runs everything, and any code you write is under its control.

So, unlike PHP, you do not write code that runs at startup. What you are doing is defining methods for the delegate, controllers, views, and other objects, as well as letting the framework use these methods as needed. You will never see a common “flow of control” throughout the program; you will only see this when control flows into your parts of the program.

This may be confusing at first, as you are trying to figure out how to trick the structure into calling your code at times and in the order you expect, but ultimately it actually simplifies the process, since you can trust the foundation to take care about a lot for you.

In a Cocoa application, most of the application logic will actually be considered by the controllers, not the application delegate. An application delegate typically performs startup and shutdown functions, but other objects do most of the work between startup and shutdown. Therefore, do not try to compress everything into an application delegate.

+8
source share

main.m contains the main() function, which is the entry point for the program, it starts first. It then calls UIApplicationMain() , which runs the OS-specific applications, and loads the main Interface Builder .xib , which contains the application delegate instance.

That is, without main.m your application delegate will not even be loaded.

+11
source share

All Articles