gpt4 book ai didi

ios - 将 RxSwift 链接到命令行工具的正确方法

转载 作者:行者123 更新时间:2023-11-28 05:46:54 26 4
gpt4 key购买 nike

我正在尝试为命令行工具构建 RxSwift,但它似乎比使用 iOS 应用程序要困难得多。

我创建了一个新的命令行项目并使用 pod 安装了 RxSwift

$ cat Podfile
# Podfile
use_frameworks!

target 'HelloRx' do
pod 'RxSwift', '~> 4.0'
end

$ pod --version
1.5.3

X代码 10.1

在打开工作区 (.xcworkspace) 且未添加任何代码的情况下,项目构建正常但在运行时崩溃:

dyld: Library not loaded: @rpath/RxAtomic.framework/Versions/A/RxAtomic
Referenced from: /Users/luke/Library/Developer/Xcode/DerivedData/HelloRx-ftbkqquhoytidfesyxazbaovndbu/Build/Products/Debug/HelloRx
Reason: image not found

动态依赖对二进制文件不可见。

$ otool -l HelloRx | grep -A 2 RPATH | grep path
path @executable_path/../Frameworks (offset 12)
path @loader_path/Frameworks (offset 12)
path @executable_path/../Frameworks (offset 12)
path @loader_path/Frameworks (offset 12)

XCode 假定二进制文件可以在相对于二进制文件的 Frameworks 目录中找到框架。不幸的是,如果我查看构建目录,则没有 Frameworks 目录,因此会出现错误。

$ ls
HelloRx Pods_HelloRx.framework RxCocoa
HelloRx.swiftmodule RxAtomic RxSwift

$ ls ..
Debug

为了让它更加困惑,所有框架都被复制到自己的 Rx* 目录中,而不是一个全局的 Frameworks 目录中。

我可以通过在“Build Settings”>>“Runpath Search Paths”中添加更多路径来解决这个问题。

'@executable_path/RxAtomic'
'@executable_path/RxSwift'

我成功了,但二进制文件仍然崩溃。

dyld: Library not loaded: @rpath/libswiftAppKit.dylib
Referenced from: /Users/luke/Library/Developer/Xcode/DerivedData/HelloRx-ftbkqquhoytidfesyxazbaovndbu/Build/Products/Debug/RxSwift/RxSwift.framework/Versions/A/RxSwift
Reason: image not found

现在是缺少 libswiftAppKit.dylib 的 RxSwift。

这可以通过添加另一个 Runpath 路径来“修复”。

'/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx'

这终于停止了崩溃,但我的应用程序抛出了很多警告:

objc[64025]: Class _TtCE6AppKitVSo17NSAnimationEffectP33_9E6F1C1DB126EBCC5B18B8BAC8A387CC26_CompletionHandlerDelegate is implemented in both /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftAppKit.dylib (0x101360b98) and /Users/luke/Library/Developer/Xcode/DerivedData/HelloRx-ftbkqquhoytidfesyxazbaovndbu/Build/Products/Debug/HelloRx (0x10059a250). One of the two will be used. Which one is undefined.
objc[64025]: Class _TtC8Dispatch16DispatchWorkItem is implemented in both /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftDispatch.dylib (0x101a7c6d0) and /Users/luke/Library/Developer/Xcode/DerivedData/HelloRx-ftbkqquhoytidfesyxazbaovndbu/Build/Products/Debug/HelloRx (0x10059bd28). One of the two will be used. Which one is undefined.
objc[64025]: Class _TtC10FoundationP33_45BFD3D387700B862E3A7353B97EF7ED20_CharacterSetStorage is implemented in both /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftFoundation.dylib (0x101c34f00) and /Users/luke/Library/Developer/Xcode/DerivedData/HelloRx-ftbkqquhoytidfesyxazbaovndbu/Build/Products/Debug/HelloRx (0x10059d5e8). One of the two will be used. Which one is undefined.
objc[64025]: Class _TtC10Foundation12_DataStorage is implemented in both /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftFoundation.dylib (0x101c34fa8) and /Users/luke/Library/Developer/Xcode/DerivedData/HelloRx-ftbkqquhoytidfesyxazbaovndbu/Build/Products/Debug/HelloRx (0x10059d690). One of the two will be used. Which one is undefined.
...

我可以接受警告,但这显然不是正确的解决方案。这让我思考,解决这个问题的正确方法是什么?

(我是 XCode 和 Swift 的新手,所以也许我正在做一些疯狂的事情)

最佳答案

一种快速的方法是将所有的 Pod 框架作为静态库。

  1. 在 Project Navigator 中选择 Pods 项目
  2. 选择所有 Pod 目标之上的主 Pod 项目
  3. 转到 Build Settings 并将 Mach-O Type 更改为 Static Library。这会将所有 pod 更改为具有 Mach-O Type 作为 Static Library。 (每次运行 pod install 时都会改回原样,因此您可能需要重新执行)
  4. 清洁

此方法的优点是您的输出将是单个可执行文件。

如果您想使用动态框架,请遵循本教程: https://medium.com/livefront/how-to-add-a-dynamic-swift-framework-to-a-command-line-tool-bab6426d6c31 .通过使用第二种方法,您的输出将不仅仅是一个可执行文件,而且您还必须提供所有动态框架。

关于ios - 将 RxSwift 链接到命令行工具的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54308137/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com