惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
美团技术团队
Recent Announcements
Recent Announcements
B
Blog
GbyAI
GbyAI
雷峰网
雷峰网
博客园_首页
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Tailwind CSS Blog
M
MIT News - Artificial intelligence
V
V2EX
人人都是产品经理
人人都是产品经理
爱范儿
爱范儿
L
LangChain Blog
Microsoft Security Blog
Microsoft Security Blog
宝玉的分享
宝玉的分享
A
About on SuperTechFans
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
U
Unit 42
Hugging Face - Blog
Hugging Face - Blog
F
Fortinet All Blogs
N
Netflix TechBlog - Medium
Last Week in AI
Last Week in AI
aimingoo的专栏
aimingoo的专栏

博客园 - 裸奔的小鸟

Screen Pen FAQ StoreAssets FAQ Heart Beat FAQ Learn Chinese FAQ 天天记单词 FAQ Swift 面向协议编程的一点见解 Swift 模块化框架的实现以及模块之间的解藕 Swift 简单工厂模式、工厂方法模式、抽象工厂模式解析 ScriptToApp FAQ Eye Protection FAQ Live Score FAQ Quick Search FAQ iPhone Screen FAQ Quick Launcher FAQ Zoom Me FAQ Show Desktop Pro FAQ The Startup Manager FAQ Finder Quick Menu FAQ The App Locker FAQ 【转载】在block中使用weakSelf/strongSelf
Swift 项目中添加main方法
裸奔的小鸟 · 2020-04-01 · via 博客园 - 裸奔的小鸟

接上一篇文章 Swift 模块化框架的实现以及模块之间的解藕 之后, 我们近一步让app在启动之前就调用ModuleManager的初始化方法. 

在OC中是有一个main.m文件以及一个main函数的,但是在swift中被去掉了,取而代之的是在AppDelegate.m中使用 @UIApplicationMain.

因此,想要实现这个功能就必须手动给它添加一个main方法

在项目中添加main方法

  1. 首先注释掉AppDelegate.m中的@UIApplicationMain
  2. 添加一个main.swift文件,并在文件中添加如下代码
import UIKit
import Foundation
import ModuleManger

private let pointer = UnsafeMutableRawPointer(CommandLine.unsafeArgv).bindMemory(
    to: UnsafeMutablePointer<Int8>?.self,
    capacity: Int(CommandLine.argc)
)


let result = ModuleManagerApplicationMain(
    CommandLine.argc,
    pointer,
    NSStringFromClass(UIApplication.self),
    NSStringFromClass(AppDelegate.self)
)

  3.在ModuleManager中添加入口函数

// app 入口函数
public func ModuleManagerApplicationMain(_ argc: Int32, _ argv: UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>!, _ principalClassName: String?, _ delegateClassName: String?) -> Int32 {
    
    ModuleManager.sharedInstance.setup()
    return UIApplicationMain(argc,argv,principalClassName,delegateClassName)
}