为什么 Xcode 使用 NSStringFromClass([AppDelegate class]) 而不是 @"AppDelegate" 或 nil

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14537202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:38:04  来源:igfitidea点击:

Why does Xcode use NSStringFromClass([AppDelegate class]) instead of just @"AppDelegate" or nil

objective-cxcode

提问by Toby

I've noticed in recent versions of Xcode where ARC is used by default, the main.m file Xcode generates for you when you start a new project uses NSStringFromClass([AppDelegate class])as the parameter for the app delegate in UIApplicationMain instead of just @"AppDelegate"or even just nil

我注意到在默认情况下使用 ARC 的最新版本的 Xcode 中,当您启动一个新项目时,Xcode 为您生成的 main.m 文件NSStringFromClass([AppDelegate class])用作 UIApplicationMain 中应用程序委托的参数,而不是仅仅@"AppDelegate"或什至只是nil

New way:

新方法:

@autoreleasepool {
      return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}

Old way:

旧方式:

  @autoreleasepool {
      int retVal = UIApplicationMain(argc, argv, nil, nil);
      return retVal;
  }

I'm just wondering if there is a reason for this? It just seems a bit contrived to me, but I'm hoping to be set straight.

我只是想知道这是否有原因?这对我来说似乎有点做作,但我希望能直截了当。

回答by Sulthan

It's a compilation check. It's better if the argument can be checked at compile-time. If it's just a string, it's impossible to check.

这是一个编译检查。如果可以在编译时检查参数会更好。如果它只是一个字符串,则无法检查。

Regarding the nilargument, the documentation says:

关于nil论点,文档说:

Specify nil if you load the delegate object from your application's main nib file.

如果从应用程序的主 nib 文件加载委托对象,请指定 nil。

That supposes you are using a xib file to declare the class of your application delegate. Well, many projects don't. In general, project templates without xib files (e.g. "Empty Application") can't use nil.

假设您正在使用 xib 文件来声明应用程序委托的类。嗯,很多项目没有。通常,没有 xib 文件的项目模板(例如“空应用程序”)不能使用nil.