我计划创建 swift 静态库并在 ios swift 应用程序中使用。我创建了一个名为 SimpleLib 的快速静态库,它包含一个返回 Hello World 字符串的公共类 Greeting:
//
// Greeting.swift
// SimpleLib
//
import Foundation
public class Greeting {
public func Hello() -> String {
return "Hello World";
}
public init() {
}
public static func SayMorning() -> String{
return "Hi, Morning";
}
}
swift 静态库项目如下所示:
module.modulemap 定义如下:
module SimpleLib {
header "SimpleLib-Swift.h"
export *
}
我构建并生成了一个 libSimpleLib.a 文件,我将 .a 和其他文件(互联网上的其他帖子提到需要放在 app 文件夹中)放在 app 文件夹中:
在应用程序项目中,我在 FREAMEWORK_SEARCH_PATHS、LIBRARY_SEARCH_PATHS 和 HEADER_SEARCH_PATHS 中包含 Libs 路径,并在 Linked Framework 中包含 .a 文件
但是,当我尝试在 AppDelegate 中引用 Greeting 类时,出现错误 - Use of unresolved identifier 'Greeting'
//
// AppDelegate.swift
// testStatic
//
import UIKit
import SimpleLib
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
var s = Greeting
return true
}
......
}
如何在静态库中制作 Swift 对象可以参考 App。在 swift 静态库中导出类/函数的正确步骤是什么?有人成功在 iOS 应用程序中构建和使用 swift 静态库吗?