Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
596 views
in Technique[技术] by (71.8m points)

ios - 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?
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...