Swiftのmain.swiftと@UIApplicationMain
3380 ワード
ニャンコさんの「100個Swift開発必須Tip」の内容は「Tip 43@UIApplicationMain」から C系言語では、プログラムの入り口は
========================================== but swiftプロジェクトではmainファイルも 推測:このラベルの役割は、表示されたクラスを依頼としてUIAPplicationを作成し、アプリケーションを起動することです.コンパイル時に、コンパイラはこのタグのクラスを探し、main関数のようなモジュールコードを自動的に挿入し、OCと同様の効果を実現します.
@UIApplicationMain
注記@UIAPplicationMainタグを外しますか?
プログラムのコンパイルが通らず、直接エラーを報告します!
main関数が見つからないという意味です
ではmainを作成してみましょうswiftファイル、OCと似たようなコードが書かれていますこのとき@UIAPplicationMainタグを再度開くとコンパイルエラー:
一山に二虎は許されない!ほほほ!!!
自分で作ったmainである以上.swiftファイルでは、どのようなことができますか?
イベントをグローバルにリスニングできます UIApplicationMainメソッド署名は、次のとおりです: 第3パラメータ説明:nilが入力された場合、UIApplication が使用される
では、もし私がnilに転送しなければ、カスタムクラスMyApplicationに転送しますか?カスタムクラスがインポートされると、クラス内で親クラスを書き換える方法を使用して、ブロック操作を行い、イベントをリスニングできます.
main
関数で、よく知っているOC APPプロジェクトに対して、Xcodeは自動的にmainを新規作成してくれました.mファイル、main関数:int main(int argc, char * argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
==========================================
main
関数も見つかりませんでした.唯一mainと関係があるのはデフォルトのAPPDelegateクラスの申明の上に@UIAPplicationMainのラベルがあります.@UIApplicationMain
注記@UIAPplicationMainタグを外しますか?
プログラムのコンパイルが通らず、直接エラーを報告します!
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
main関数が見つからないという意味です
ではmainを作成してみましょうswiftファイル、OCと似たようなコードが書かれています
import UIKit
class MyApplication: UIApplication {
}
UIApplicationMain(Process.argc, Process.unsafeArgv, nil, NSStringFromClass(AppDelegate))
'UIApplicationMain' attribute cannot be used in a module that contains top-level code
一山に二虎は許されない!ほほほ!!!
自分で作ったmainである以上.swiftファイルでは、どのようなことができますか?
イベントをグローバルにリスニングできます
/// This function is called in the main entry point to create the application object and the application delegate and set up the event cycle. Even though an integer return type is specified, this function never returns. When users exits an iOS application by pressing the Home button, the application moves to the background.
///
/// @param argc The count of arguments in argv; this usually is the corresponding parameter to main.
/// @param argv A variable list of arguments; this usually is the corresponding parameter to main.
/// @param principalClassName The name of the UIApplication class or subclass. If you specify nil, UIApplication is assumed.
/// @param delegateClassName designates a subclass of UIApplication, you may designate the subclass as the delegate; the subclass instance receives the application-delegate messages. Specify nil if you load the delegate object from your application’s main nib file.
///
/// @return Even though an integer return type is specified, this function never returns.
public func UIApplicationMain(argc: Int32, _ argv: UnsafeMutablePointer>, _ principalClassName: String?, _ delegateClassName: String?) -> Int32
では、もし私がnilに転送しなければ、カスタムクラスMyApplicationに転送しますか?
import UIKit
class MyApplication: UIApplication {
override func sendEvent(event: UIEvent) {
super.sendEvent(event)
print("sendEvent")
}
}
// nil
UIApplicationMain(Process.argc, Process.unsafeArgv, NSStringFromClass(MyApplication), NSStringFromClass(AppDelegate))