Swift iOS应用程序从何处开始? [英] Where does a Swift iOS application begin its life?
问题描述
如果我在Xcode中创建一个Objective-C iOS应用程序,则会生成一个名为 main.m
的文件。该文件的内容如下所示:
If I create an Objective-C iOS application in Xcode, a file named main.m
is generated. The contents of the file look something like this:
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int main(int argc, char * argv[]) {
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
这是Objective-C iOS应用程序开始的地方它的生命。
And this is where an Objective-C iOS application begins its life.
重要的是,如果我想继承 UIApplication
(无论出于何种原因),那么这里是我去告诉我的应用程序用于应用程序类的类。同样,如果由于某种原因我想为我的应用程序使用 AppDelegate
以外的类名,我会在这里更改这些信息。
Importantly, if I want to subclass UIApplication
(for whatever reason), then here is where I go to tell my app which class to use for the application class. Likewise, if for some reason I want to use a class name other than AppDelegate
for my app, I'd change that information here.
HOWEVER ,如果我在Xcode中创建Swift iOS应用程序,则不会生成此类文件(我已经能够找到)。我在哪里设置这些东西?
HOWEVER, if I create a Swift iOS application in Xcode, no such file is generated (that I've been able to find). Where do I set these things up at?
推荐答案
在Swift中你可以使用 @UIApplicationMain 您的应用委托类的code>属性,它将自动为您调用
UIApplicationMain()
函数。
In Swift you can use the @UIApplicationMain
attribute on your app delegate class, and it will automatically call the UIApplicationMain()
function for you.
@UIApplicationMain
class YourAppDelegate: UIResponder, UIApplicationDelegate { ...
还有另一种选择:
如果你不使用这个属性,请提供一个带有a的main.swift文件调用
UIApplicationMain
函数的main
函数。例如,如果您的应用使用自定义子类UIApplication
作为其主要类,请调用UIApplicationMain
函数而不是使用此属性。
If you do not use this attribute, supply a main.swift file with a
main
function that calls theUIApplicationMain
function. For example, if your app uses a custom subclass ofUIApplication
as its principal class, call theUIApplicationMain
function instead of using this attribute.
参考文献:
- The Swift Programming Language: Attributes
- What does "@UIApplicationMain" mean?
这篇关于Swift iOS应用程序从何处开始?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!