Let's go back a little.
Square brackets ( [ ] ) are the syntax for invoking the Objective-C method. Therefore, if Cocoa had C # syntax, the equivalent syntax would be as follows:
TodoAppDelegate appDelegate = UIApplication.sharedApplication.delegate;
In C #, you should use a static class for a class that has only one instance. Cocoa uses the Singleton pattern to achieve this. The class method (in this case, sharedApplication ) is used to retrieve a single instance of this class.
Delegates in Cocoa are not like the delegate keyword in C #, so don't confuse this. In C #, the delegate keyword is used to refer to a method. Cocoa's delegate template is provided as an alternative to the subclass.
Many objects allow you to specify another object as a delegate. Delegates implement the methods that these objects will call to notify about specific events. In this case, UIApplication is the class that represents the currently running application (for example, similar to System.Windows.Forms.Application ). It sends messages to its delegate when events occur that affect the application (for example, when the application starts, quits, receives or loses focus, etc.)
Another concept of Objective-C is protocol . In principle, this is similar to the .NET interface , except that methods can be marked as @optional , which means that classes are not required to implement methods marked in this way. The delegates in the iPhone SDK are simply objects that conform to a particular protocol. In the case of UIApplication , protocol delegates must conform to UIApplicationDelegate .
Since this is not required to implement each method, it gives the delegate the flexibility to choose the methods that are worth implementing. If you want, for example, to perform some actions when the application finishes launching, you can implement the class that conforms to the UIApplicationDelegate protocol, set it as an instance of the UIApplication delegate , and then implement applicationDidFinishLaunching:
UIApplication will determine if its delegate implements this method when the application finishes launching, and if so, call this method. This gives you the ability to respond to this event without subclassing UIApplication .
In iPhone applications, developers also often use the application delegate as a kind of top-level object. Since you usually don’t use the UIApplication subclass, most developers save their global application data in the application’s split.