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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…