Swift中的main.swift與@UIApplicationMain

感謝喵神的《100個Swift開發(fā)必備 Tip》 內(nèi)容參考自 “Tip43 @UIApplicationMain”

  • 在C系語言中,程序的入口都是 main 函數(shù),對于熟悉的 OC APP 項目,Xcode自動幫我們新建了一個 main.m 文件,其中就有 main 函數(shù):
int main(int argc, char * argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

======================== 樸實無華的分割線 =====================

  • but 在 swift 項目里,不曾找到 main 文件,也沒有 main 函數(shù)。唯一和 main 有關(guān)系的是默認的 APPDelegate 類的申明上方有個 @UIApplicationMain 的標簽。
  • 猜測:這個標簽的作用就是將被標注的類作為委托,去創(chuàng)建一個
    UIApplication 并啟動應(yīng)用程序。在編譯的時候,編譯器將尋找這個標記的類,并自動插入像 main 函數(shù)這樣的模塊代碼,從而實現(xiàn)和 OC 類似的效果。

@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)

意思應(yīng)該是找不到main函數(shù)

那么我們可以嘗試一下,創(chuàng)建一個main.swift文件,里面寫上和OC類似的代碼

import UIKit

class MyApplication: UIApplication {
    
}

UIApplicationMain(Process.argc, Process.unsafeArgv, nil, NSStringFromClass(AppDelegate))
  • 此時,如果再打開@UIApplicationMain標記,編譯就報錯了:
'UIApplicationMain' attribute cannot be used in a module that contains top-level code

一山不能容二虎!呵呵!!!

既然是自己創(chuàng)建的main.swift文件,那么我們可以用它做哪些事情呢?

可以全局監(jiān)聽事件

  • UIApplicationMain方法簽名如下:
///  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<UnsafeMutablePointer<Int8>>, _ principalClassName: String?, _ delegateClassName: String?) -> Int32
  • 第三個參數(shù)說明:如果傳入nil,則使用UIApplication

那么如果我不傳入nil,傳入自定義的類MyApplication呢?

  • 如果傳入自定義的類,那么我們就可以在類中重寫父類的方法,做一些攔截性操作,監(jiān)聽某些事件了!
import UIKit

class MyApplication: UIApplication {
    override func sendEvent(event: UIEvent) {
        super.sendEvent(event)
        print("sendEvent")
    }
}
// 第三個參數(shù)不要傳nil
UIApplicationMain(Process.argc, Process.unsafeArgv, NSStringFromClass(MyApplication), NSStringFromClass(AppDelegate))
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 問題 1)柯里化,通過柯里化,改造target-action,因為selector只能使用字符串,在編譯時無法發(fā)現(xiàn)...
    lanjing閱讀 3,544評論 3 19
  • *面試心聲:其實這些題本人都沒怎么背,但是在上海 兩周半 面了大約10家 收到差不多3個offer,總結(jié)起來就是把...
    Dove_iOS閱讀 27,210評論 30 471
  • Swift 介紹 簡介 Swift 語言由蘋果公司在 2014 年推出,用來撰寫 OS X 和 iOS 應(yīng)用程序 ...
    大L君閱讀 3,301評論 3 25
  • 計算器屋子實驗和中文屋子實驗很像。 假設(shè)有個盒子,里面有個機器,每次當我輸入一些合法數(shù)字計算符號,它就會輸出一個正...
    你看得見我閱讀 505評論 23 4
  • 長安的九月,長青樹木春意盎然。陳清和用一把冷水洗去早起的倦意,就像是被大雨沖洗的楓樹現(xiàn)在變得精神抖擻。他透過那...
    南蒿閱讀 224評論 0 0