ios 静态库和 Swift
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29676470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Static Library and Swift
提问by Que20
So I'm working on an iOS project in Swift, and I wanted to create a Static library with some useful stuff in it.
所以我正在 Swift 中开发一个 iOS 项目,我想创建一个包含一些有用东西的静态库。
My problem is when I try to build my lib in Xcode (version 6.3) I have a "Build Failed" followed by : /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: unknown option character 'X' in: -Xlinker
我的问题是当我尝试在 Xcode(版本 6.3)中构建我的库时,我有一个“构建失败”,然后是: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: unknown option character 'X' in: -Xlinker
I've never saw this and it's not my first static lib. So I was thinking I may be linked to the fact that I'm using exclusively Swift class.
我从未见过这个,这不是我的第一个静态库。所以我想我可能与我只使用 Swift 类的事实有关。
What do you guys think ? Thank you in advance.
你们有什么感想 ?先感谢您。
采纳答案by Alessandro
As of Xcode 9 beta 4, Xcode natively supports static libraries with Swift sources.
从 Xcode 9 beta 4 开始,Xcode 原生支持带有 Swift 源代码的静态库。
回答by Dean Kelly
As mentioned, Apple doesallow Swift in static libraries as of Xcode 9 Beta 4.
如前所述,从 Xcode 9 Beta 4 开始,Apple确实允许在静态库中使用 Swift。
We attempted to do this on an existing project with an Objective-C-based target and "child" static library projects and kept running into a linking error
我们尝试在现有项目上执行此操作,该项目具有基于 Objective-C 的目标和“子”静态库项目,但一直遇到链接错误
ld: library not found for -lswiftSwiftOnoneSupport for architecture x86_64
also
还
ld: library not found for -lswiftDispatch for architecture x86_64
This is because the main target (app) is trying to build solely against Objective-C and isn't told by the static library that it needs to include Swift libraries. This was because there weren't any Swift files in the Compile Sources
section of our Build Phases
for the app target.
这是因为主要目标(应用程序)试图仅针对 Objective-C 进行构建,并且静态库并未告知它需要包含 Swift 库。这是因为在Compile Sources
我们Build Phases
的 app 目标部分中没有任何 Swift 文件。
So basicallyall you have to do is add at least one .swift
file to that compile list and it will include the Swift libraries for you. It doesn't even need to have any code or values in it, it can be an empty file.
所以基本上你所要做的就是将至少一个.swift
文件添加到该编译列表中,它会为你包含 Swift 库。它甚至不需要任何代码或值,它可以是一个空文件。
Then you can start adding Swift files to your "child" static library project. I would let it generate the bridging header for you at first then you can move it around and change what gets imported (make sure the project points to the right file in the build settings if you move it).
然后您可以开始将 Swift 文件添加到您的“子”静态库项目中。我会让它首先为您生成桥接头,然后您可以移动它并更改导入的内容(如果移动它,请确保项目指向构建设置中的正确文件)。
You should still keep in mind that using Swift and Objective-C within the samestatic library may have issues of its own. I suggest reading the Apple developer doc "Swift and Objective-C in the Same Project"on how to address importing Objective-C into Swift (using a bridging header) and how to use the Swift files in your Objective-C code (importing the generated -Swift.h
for your library).
您仍然应该记住,在同一个静态库中使用 Swift 和 Objective-C可能有其自身的问题。我建议阅读Apple 开发人员文档“同一项目中的 Swift 和 Objective-C”,了解如何将 Objective-C 导入 Swift(使用桥接头)以及如何在 Objective-C 代码中使用 Swift 文件(导入-Swift.h
为您的库生成)。
回答by Giordano Scalzo
Swift doesn't support static library
Swift 不支持静态库
Although the correct way should be create a framework, there is a workaround here.
虽然正确的方法应该是创建一个框架,但这里有一个解决方法。
回答by yoAlex5
Swift consumer -> Swift static library
Swift 消费者 -> Swift 静态库
Xcode version 10.2.1
Xcode 版本 10.2.1
Create Swift static library
创建 Swift 静态库
Create a library project or create a library target
创建库项目或创建库目标
File -> New -> Project... -> Cocoa Touch Static Library
//or
Project editor -> Add a Target -> Cocoa Touch Static Library
Add files .swift
添加文件 .swift
Select `.swift` file -> Select File Inspectors Tab -> Target Membership -> Select the target
//or
Project editor -> select a target -> Build Phases -> Compile Sources -> add files
Build library - ? Command+ Bor Product -> Build
构建库 - ? Command+B或Product -> Build
Note 1: Be sure that you build library for the same process architecture as the client code.
Note 2: expose your API that should be visible for consumer using public
or open
access modifiers[About]
注意 1:确保为与客户端代码相同的流程架构构建库。
注意 2:公开您的 API,该 API 应该对消费者使用public
或open
访问修饰符可见[关于]
Find generated output[Build location]
查找生成的输出[构建位置]
Products group -> lib<product_name>.a -> Show in Finder
The directory includes
该目录包括
lib<product_name>.a
– a built static library<product_name>.swiftmodule
.swiftmodule
describe an interface of a library and a compiler version. This folder includes:.swiftdoc
- docs.swiftmodule
- public interface/definitions
lib<product_name>.a
– 一个内置的静态库<product_name>.swiftmodule
.swiftmodule
描述库的接口和编译器版本。该文件夹包括:.swiftdoc
- 文档.swiftmodule
- 公共接口/定义
Swift consumer with Swift static library
带有 Swift 静态库的 Swift 使用者
Drag and drop
the binary into the Xcode project[About]
Drag and drop
将二进制文件导入 Xcode 项目[关于]
Link Binary
[Undefined symbols][Link vs Embed]
Project editor -> select a target -> General -> Linked Frameworks and Libraries -> add -> Add Others... -> point to `lib<target_name>.a` file
//or
Project editor -> select a target -> Build Phases -> Link Binary With Libraries -> add -> Add Others... -> point to `lib<target_name>.a` file
Add Library Search paths
[Library not found for][Recursive path]
添加Library Search paths
[未找到库] [递归路径]
Project editor -> select a target -> Build Settings -> Search Paths -> Library Search paths -> add path to the parent of `lib<target_name>.a` file
Add Import Paths
[No such module][Recursive path]
Project editor -> select a target -> Build Settings -> Swift Compiler - Search Paths -> Import Paths -> add path to a folder with `.swiftmodule`
Import module to the Swift client code [module_name]
将模块导入 Swift 客户端代码[module_name]
import module_name