从快速包装内部使用吊舱 [英] Using a pod from inside a swift package

查看:85
本文介绍了从快速包装内部使用吊舱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过使用 Swift Package Manager 将应用程序的各个部分分成不同的层(包):演示,业务和数据,以使其尽可能整洁,从而使我的应用程序模块化.在项目设置中将那些手动创建的程序包与项目链接->框架,库和嵌入式内容部分.但是,在项目的某些部分中,我仍然需要 Cocoa Pods ,为简单起见,我的Pod文件包含以下内容:

I'm making my app modularized using Swift Package Manager by separating various parts of the app into different layers (packages): Presentation, Business and Data to make it as clean as possible, and I link those manually created packages with the project in project settings -> Frameworks, Libraries and Embedded Content section. However, I'm still in need for Cocoa Pods in some parts of the project, for simplicity my pod file contains the following:

target 'SampleApp' do
  use_frameworks!
  
  pod 'Localize-Swift'
end

当我将 Localize-Swift 导入到那些软件包的源文件之一中时,出现错误:没有这样的模块,但是Pods导入在未打包的情况下效果很好应用程序的某些部分,例如 SceneDelegate .我知道 SPM 依赖项必须仅是Swift软件包,但是是否有任何已知的解决方法将pod链接到swift软件包中?

when I import the Localize-Swift into one of those packages' source files I get the error: No such module, however Pods import works well in non-packaged parts of the app like SceneDelegate. I know that SPM dependencies must be Swift Packages only, but is there any known workaround to link pods into swift packages ?

推荐答案

可能,但是它没有用,因为Swift程序包不像XCode项目使用-那样弱链接到框架-weak_framework< framework_name> ,因此您需要实施一些非常规的步骤.

It's possible but it's not useful because Swift Packages don't have weak linking to frameworks as XCode project does with -weak_framework <framework_name> so you need to implement non trivial steps.

在swift包中,您可以指定要与 linkedFramework 链接的框架:

In swift package you can specify a framework to link with linkedFramework:

.target(
    name: "MyPackage",
    dependencies: [],
    linkerSettings: [
        .linkedFramework("Localize_Swift")
        .unsafeFlags(["-F/Users/user/.../Localize-Swift"])
    ]
),

其中 unsafeFlags 指定框架的dir的完整路径.但是出乎意料的是,由于下一个问题,您不能将包与您的应用程序一起使用:

Where unsafeFlags specifies the full path to dir with the framework. But unexpectedly then you can not use your package with your app because of next issue:

包装产品'MyPackage'不能用作此产品的依赖项定位,因为它使用了不安全的构建标志.

The package product 'MyPackage' cannot be used as a dependency of this target because it uses unsafe build flags.

要使用您的框架编译Swift软件包,您应该将Localize_Swift.framework复制到目标软件包的构建目录,然后可以省略该软件包的不安全构建标志,因为编译器会在该目录的根目录下看到所有依赖项.

To compile Swift Package with your framework you should copy Localize_Swift.framework to target build dir of your package and then you can omit unsafe build flags of the package because the compiler sees all dependencies on the dir's root level.

.target(
    name: "MyPackage",
    dependencies: [],
    linkerSettings: [
        .linkedFramework("Localize_Swift")
    ]
),

添加软件包后,您可以对应用执行相同的操作.如果将Localize_Swift.framework复制到应用程序的目标构建目录中,则可以编译链接的swift程序包,因为它会在当前构建目录中查找 linkedFramework .

The same you can do with your app after adding your package. If you copy Localize_Swift.framework to the app's target build dir then your linked swift package can be compiled because it looks for linkedFramework in the current build dir.

默认情况下,会生成默认Pod来分离目标构建目录中的文件夹,例如: $ TARGET_BUILD_DIR/Localize-Swift/Localize_Swift.framework ,因此您可以将 CONFIGURATION_BUILD_DIR 更改为Pods项目中的Localise-Swift 目标可将框架生成到目标构建目录的根目录或创建要复制的脚本等.但是存在一个问题,即swift软件包依赖项在编译过程的早期开始就开始编译当您没有任何已编译的pod框架时.因此,在第一阶段,您应该先编译Localize_Swift.framework(最好制作一个胖框架),然后在 Build <下添加 Pre-actions Run Script /code>的目标方案中,将框架从目标复制到目标构建目录.

By default pods are generated to separate folders in target build dir e.g.: $TARGET_BUILD_DIR/Localize-Swift/Localize_Swift.framework so you can change CONFIGURATION_BUILD_DIR for Localise-Swift target in Pods project to generate the framework to the root of the target build dir or make a script to copy etc. But there is a problem that swift package dependencies are started compiling at the early begin of compilation process when you don't have any compiled pod frameworks. So at the first stage you should compile your Localize_Swift.framework before (better to make a fat framework) and then add a Pre-actions Run Script under Build in your target scheme that copies the framework from your destination to target build dir.

cp -r $PROJECT_DIR/Localize_Swift.framework $TARGET_BUILD_DIR/Localize_Swift.framework

现在,您的应用程序和swift包都可以使用Localize_Swift框架进行编译了.

Now both your app and your swift package can be compiled with Localize_Swift framework.

正如我在一开始所说的那样,它没有用,因为您需要在常规编译过程之前手动(或使用其他脚本)编译Localize_Swift.framework,以完全抵消可可豆的方便性.

As I say in the begin it's not useful because you need to compile Localize_Swift.framework manually (or with an additional script) before the general compilation process that neutralizes convenience of cocoa pods at all.

考虑使用下一个首选选项:

Consider using next preferred options:

  1. Pod可以作为快速包提供..您可以在包中建立依赖关系,在这种情况下,您的包和依赖关系也将在您的应用程序中可用.顺便说一下Localize_Swift支持swift软件包.
  1. Pod is avaliable as a swift package. You can make a dependency in your package and in this case both your package and the dependency will be also available in your app. By the way Localize_Swift supports swift packages.

  1. Pod具有快速的源代码.您可以使用源文件制作自己的swift软件包,然后将其链接到您的软件包.

  1. Pod has swift sources You can make your own swift package with the sources files and then link it to your package.

Pod具有二进制文件从Swift 5.3中,swift软件包可以嵌入 xcframework ,因此您可以从pod的二进制文件构建此xcframework,然后在swift软件包中建立二进制目标依赖项: .binaryTarget(name:"MyLib",路径:"MyLib.xcframework")(请参见

Pod has binaries From Swift 5.3 swift package can embed xcframework so you can build this xcframework from pod's binaries and then make binary target dependency in your swift package: .binaryTarget(name: "MyLib", path: "MyLib.xcframework") (see How can I add a local library as a dependency in Swift Package Manager)

这篇关于从快速包装内部使用吊舱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆