Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return Type of -application:didFinishLaunchingWithOptions:

When developing for iOS, the first entry point for your app is the -[AppDelegate application:didFinishLaunchingWithOptions:]. The return type of this method is a BOOL. By default, the return type of this method is YES. Here is the code automatically generated by Xcode.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];     // Override point for customization after application launch.     self.window.backgroundColor = [UIColor whiteColor];     [self.window makeKeyAndVisible];      return YES; } 

As you can see, Xcode puts in the return statement for you, with the value of YES. When I change the value of the return statement to NO, and don't change anything else, nothing happens. The app doesn't quit or show any unusual behavior. This begs the question, what is the purpose of the method returning a BOOL, when the returned value doesn't matter? If the value returned doesn't matter, why doesn't the method just return void?
Note: Some of my expectations after changing the return to NO were either

  • Application doesn't launch because it doesn't receive "permission" (NO is returned)
  • Either compiler generates warning, or error is raised at runtime.
    Why is it the case that neither of these things happen?
like image 303
Brian Tracy Avatar asked Apr 13 '14 03:04

Brian Tracy


People also ask

What is return type of method?

A return statement causes the program control to transfer back to the caller of a method. Every method in Java is declared with a return type and it is mandatory for all java methods. A return type may be a primitive type like int, float, double, a reference type or void type(returns nothing).

How do you determine the return type of a function?

Use the ReturnType utility type to get the return type of a function in TypeScript, e.g. type T = ReturnType<typeof myFunction> . The ReturnType utility type constructs a type that consists of the return type of the provided function type.

What does () => void mean TypeScript?

Introduction to TypeScript void type The void type denotes the absence of having any type at all. It is a little like the opposite of the any type. Typically, you use the void type as the return type of functions that do not return a value.

What does ?: Mean in TypeScript?

Using ?: with undefined as type definition While there are no errors with this interface definition, it is inferred the property value could undefined without explicitly defining the property type as undefined . In case the middleName property doesn't get a value, by default, its value will be undefined .


1 Answers

If there is a URL in launchOptions and you return NO, then the operating system will know that you cannot open the provided URL for some reason.

It's not used for anything else. Since usually there is no URL in launchOptions it usually doesn't matter what you return.

So just return YES. You can read more in the Apple documentation here.

like image 133
Abhi Beckert Avatar answered Sep 28 '22 03:09

Abhi Beckert