Swift-@UIApplicationMain

Swift-@UIApplicationMain,第1张

概述Swift-@UIApplicationMain 感谢喵神的《100个Swift开发必备 Tip》内容参考自 “Tip43 @UIApplicationMain” 在C系语言中,程序的入口都是main函数,对于熟悉的OC APP项目,Xcode自动帮我们新建了一个main.m文件,其中就有main函数: int main(int argc, char * argv[]){ @autore Swift-@UIApplicationMain

感谢喵神的《100个Swift开发必备 Tip》内容参考自 “Tip43 @UIApplicationMain”

在C系语言中,程序的入口都是main函数,对于熟悉的OC APP项目,Xcode自动帮我们新建了一个main.m文件,其中就有main函数:
int main(int argc,char * argv[]){    @autoreleasepool {        return UIApplicationMain(argc,argv,nil,NsstringFromClass([AppDelegate class]));    }}

====================== 分割线 ===================

but 在新建的swift项目里,不曾找到main文件,也没有main函数。唯一和main有关系的是默认的APPDelegate类的申明上方有个@UIApplicationMain的标签。 猜测:这个标签的作用就是将被标注的类作为委托,去创建一个UIApplication并启动应用程序。在编译的时候,编译器将寻找这个标记的类,并自动插入像main函数这样的模块代码,从而实现和OC类似的效果。 @UIApplicationMain

注释掉@UIApplicationMain标记?

程序编译不通过,直接报错!
Undefined symbols for architecture x86_64:  "_main",referenced from:     implicit entry/start for main executableld: symbol(s) not found for architecture x86_64clang: error: linker command Failed with exit code 1 (use -v to see invocation)
* 意思应该是找不到main函数

那么我们可以尝试一下,创建一个main.swift文件,里面写上和OC类似的代码

import UIKit@H_301_92@ class MyApplication: UIApplication { } UIApplicationMain(Process.argc,Process.unsafeArgv,nil,NsstringFromClass(AppDelegate))
此时,如果再打开@UIApplicationMain标记,编译就报错了:
'UIApplicationMain' attribute cannot be used in a @H_301_92@module that contains top-level code
一山不能容二虎!呵呵!!!

既然是自己创建的main.swift文件,那么我们可以用它做哪些事情呢?

* 可以全局监听事件*

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
第三个参数说明:如果传入nil,则使用UIApplication

那么如果我不传入nil,传入自定义的类MyApplication呢?

如果传入自定义的类,那么我们就可以在类中重写父类的方法,做一些拦截性 *** 作,监听某些事件了!
import UIKitclass MyApplication: UIApplication {    overrIDe func sendEvent(event: UIEvent) {        super.sendEvent(event)        print("sendEvent")    }}// 第三个参数不要传nilUIApplicationMain(Process.argc,Process.unsafeArgv,NsstringFromClass(MyApplication),NsstringFromClass(AppDelegate))
总结

以上是内存溢出为你收集整理的Swift-@UIApplicationMain全部内容,希望文章能够帮你解决Swift-@UIApplicationMain所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1075304.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存