文章目录
– OC项目中自定义 application 类
macOS
- Step 1: 创建自己的类:CustomApplication
- Step 2: 更改info.plist 文件中 Principal class 配置的类名为 CustomApplication :
iOS
- Step 1: 创建自己的类:CustomApplication
- Step 2: main 方法中传入自定义类的名称:
– swift 项目中自定义 application 类
macOS
- Step 1: 创建自己的类:ACDHubApplication
@objc(ACDHubApplication) // 必须加上这句,否则运行报错:Unable to find class: MyApplication, exiting
class ACDHubApplication: NSApplication {
override func run() {
super.run()
}
}
- Step 2: 更改info.plist 文件中 Principal class 配置的类名为 ACDHubApplication (下图蓝色框)
- Step 3: 如果有Main.storyboard, 将其中的Application 的object的class 改为 ACDHubApplication (注:这一步我测试发现不做也没问题,不过还是改了好,可能我暂时没发现问题)
题外: 自定义 swift application 的 main 函数 (macOS 工程)
现在创建的swift application 项目是没有显示的
main
函数
(下面是macos工程,iOS可以自行搜索,网上有很多)
main 函数是程序的入口
在OC里,main 函数是这样的:
int main(int argc, const char * argv[]) {
@autoreleasepool {
// Setup code that might create autoreleased objects goes here.
}
return NSApplicationMain(argc, argv);
}
到了Swift ,main 函数就“消失了”,它的存在的形式变成了@NSApplicationMain
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
}
当我们有需求,需要自定义一个main 函数的时候,删除上面的@NSApplicationMain
, 要创建一个main.swift 文件:
import Cocoa
NSApplicationMain(CommandLine.argc, CommandLine.unsafeArgv) // NSApplicationMain