【发布时间】:2019-01-16 23:17:10
【问题描述】:
我打算创建 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 静态库吗?
【问题讨论】:
-
你应该复制你的代码而不是发布截图。屏幕截图不能作为文本进行操作,这会导致很多问题。阅读您的问题的人无法复制您的代码进行尝试,依赖屏幕阅读器的人无法知道您的代码是什么。
-
我在帖子中更新并复制了我的代码。希望它可以帮助理解我的问题
标签: ios swift static-libraries xcode10.1