如何链接到第三方Swift框架

时间:2022-12-31 04:51:16

In the last 24 hours I've tried to consume two different 3rd party Swift libraries: Swiftz and Sleipnir. I assume it should be fairly easy as both projects simply provide a sketch of how to consume them. However, neither provide enough detail for me to follow. I've tried all of the following: add the projects to my workspace, drag/drop the compiled frameworks into my project navigator, add the frameworks to the "Link Binary with Libraries" phase. In all cases, I get a linker error informing me that the framework can't be found.

在过去的24小时内,我尝试使用两个不同的第三方Swift库:Swiftz和Sleipnir。我认为它应该相当容易,因为两个项目都只提供了如何使用它们的草图。但是,两者都没有为我提供足够的细节。我已经尝试了以下所有方法:将项目添加到我的工作区,将已编译的框架拖放到我的项目导航器中,将框架添加到“Link Binary with Libraries”阶段。在所有情况下,我都收到链接器错误,通知我无法找到框架。

I just need a link to the actual documentation that explains how to do this. I haven't been able to find it.

我只需要一个实际文档的链接来解释如何执行此操作。我找不到它。

Edit: Forget about 3rd party libraries even. How do you consume your own libraries? I have a Cocoa Framework library I've written. Now I've added a command line project to the same workspace. Sure would be nice if I could access my new framework from my command line app. I clicked on my command line project. Clicked on Build Phases and dragged my custom framework to the Link Binary with Libraries section. Everything compiles fine. When running my app I get the following error:

编辑:忘记第三方图书馆了。你如何消费自己的图书馆?我有一个我写过的Cocoa Framework库。现在我已将命令行项目添加到同一工作区。如果我可以从命令行应用程序访问我的新框架,那肯定会很好。我点击了我的命令行项目。单击Build Phases并将我的自定义框架拖到Link Binary with Libraries部分。一切都很好。运行我的应用程序时,我收到以下错误:

dyld: Library not loaded: @rpath/libswiftAppKit.dylib
  Referenced from: /Users/mgwelch/Library/Developer/Xcode/DerivedData/SwiftParsing-
  btkaoklayoffujgrkklfyffpipih/Build/Products/Debug/Parsing.framework/Versions/A/
  Parsing
  Reason: image not foundI

3 个解决方案

#1


6  

Ok, not many users are familiar with Swift frameworks yet, I guess.

好吧,我猜不是很多用户熟悉Swift框架。

Well, I'm getting some assistance from a framework developer over on an issue I raised on how to consumer their framework. I still have issues, but others may be interested in following the conversation to see some of the steps involved

好吧,我从一个框架开发人员那里获得了一些关于我如何消费他们的框架的问题的帮助。我仍然有问题,但其他人可能有兴趣跟进对话,看看涉及的一些步骤

https://github.com/typelift/swiftz/issues/114

https://github.com/typelift/swiftz/issues/114

Detailed instructions are provided at this issue. Feel free to try them. I'll mark this as closed.

本期提供了详细说明。随意尝试。我将此标记为已关闭。

UPDATE I stated this in a comment but it might not be seen. I was trying to write a command line tool. At this time with XCode 6.1 it does not appear possible to use a third party framework in a command line tool. As a matter of fact I've written up an issue against another project stating that their command line targets no longer run: https://github.com/railsware/Sleipnir/issues/17

更新我在评论中说明了这一点,但可能没有看到。我正在尝试编写命令行工具。此时使用XCode 6.1,似乎无法在命令行工具中使用第三方框架。事实上,我已经针对另一个项目写了一个问题,说明他们的命令行目标不再运行:https://github.com/railsware/Sleipnir/issues/17

Perhaps this worked in a previous beta version, but it no longer seems possible.

也许这在以前的测试版中有效,但似乎不再可能。

#2


2  

Try checking Runpath Search Paths in Build Settings. Check if the relative path to Frameworks directory is correct.

尝试检查构建设置中的Runpath搜索路径。检查Frameworks目录的相对路径是否正确。

LD_RUNPATH_SEARCH_PATHS = $(inherited) @executable_path/../Frameworks @loader_path/Frameworks

#3


2  

Check out the following document (Embedded Swift frameworks in OS X command line tools), it describes a process that works for embedding Swift frameworks in command line tools.

查看以下文档(OS X命令行工具中的嵌入式Swift框架),它描述了一个适用于在命令行工具中嵌入Swift框架的过程。

It's more of a work-around than a solution but it works. Ideally, the folks at Apple should put a friendly sign in large, bold, red letters when creating a command line app in Swift to avoid us wasting time with issues like this.

它更像是一种解决方案,而不是解决方案,但它有效。理想情况下,在Swift中创建命令行应用程序时,Apple的用户应该在大号粗体红色字母中添加一个友好的符号,以避免我们浪费时间来解决这类问题。

I've followed the process for Swift 1.1 and Swift 1.2 without any issues. The only tricky bit it to get argv to swift, but you can do this in objective-c in main.m with:

我已经按照Swift 1.1和Swift 1.2的流程进行了操作,没​​有任何问题。将argv转换为swift是唯一棘手的一点,但你可以在main.m中的objective-c中执行此操作:

int main(int argc, const char * argv[]) {

    @autoreleasepool {

        NSMutableArray *paramArray = [[NSMutableArray alloc] init];
        for (int i=0; i<argc ;i++) {
            [paramArray addObject:[[NSString alloc] initWithUTF8String:argv[i]]];
        }
        return (int) [[Whatever sharedInstance]manage:argc param:paramArray];
    }
    return 0;
}

#1


6  

Ok, not many users are familiar with Swift frameworks yet, I guess.

好吧,我猜不是很多用户熟悉Swift框架。

Well, I'm getting some assistance from a framework developer over on an issue I raised on how to consumer their framework. I still have issues, but others may be interested in following the conversation to see some of the steps involved

好吧,我从一个框架开发人员那里获得了一些关于我如何消费他们的框架的问题的帮助。我仍然有问题,但其他人可能有兴趣跟进对话,看看涉及的一些步骤

https://github.com/typelift/swiftz/issues/114

https://github.com/typelift/swiftz/issues/114

Detailed instructions are provided at this issue. Feel free to try them. I'll mark this as closed.

本期提供了详细说明。随意尝试。我将此标记为已关闭。

UPDATE I stated this in a comment but it might not be seen. I was trying to write a command line tool. At this time with XCode 6.1 it does not appear possible to use a third party framework in a command line tool. As a matter of fact I've written up an issue against another project stating that their command line targets no longer run: https://github.com/railsware/Sleipnir/issues/17

更新我在评论中说明了这一点,但可能没有看到。我正在尝试编写命令行工具。此时使用XCode 6.1,似乎无法在命令行工具中使用第三方框架。事实上,我已经针对另一个项目写了一个问题,说明他们的命令行目标不再运行:https://github.com/railsware/Sleipnir/issues/17

Perhaps this worked in a previous beta version, but it no longer seems possible.

也许这在以前的测试版中有效,但似乎不再可能。

#2


2  

Try checking Runpath Search Paths in Build Settings. Check if the relative path to Frameworks directory is correct.

尝试检查构建设置中的Runpath搜索路径。检查Frameworks目录的相对路径是否正确。

LD_RUNPATH_SEARCH_PATHS = $(inherited) @executable_path/../Frameworks @loader_path/Frameworks

#3


2  

Check out the following document (Embedded Swift frameworks in OS X command line tools), it describes a process that works for embedding Swift frameworks in command line tools.

查看以下文档(OS X命令行工具中的嵌入式Swift框架),它描述了一个适用于在命令行工具中嵌入Swift框架的过程。

It's more of a work-around than a solution but it works. Ideally, the folks at Apple should put a friendly sign in large, bold, red letters when creating a command line app in Swift to avoid us wasting time with issues like this.

它更像是一种解决方案,而不是解决方案,但它有效。理想情况下,在Swift中创建命令行应用程序时,Apple的用户应该在大号粗体红色字母中添加一个友好的符号,以避免我们浪费时间来解决这类问题。

I've followed the process for Swift 1.1 and Swift 1.2 without any issues. The only tricky bit it to get argv to swift, but you can do this in objective-c in main.m with:

我已经按照Swift 1.1和Swift 1.2的流程进行了操作,没​​有任何问题。将argv转换为swift是唯一棘手的一点,但你可以在main.m中的objective-c中执行此操作:

int main(int argc, const char * argv[]) {

    @autoreleasepool {

        NSMutableArray *paramArray = [[NSMutableArray alloc] init];
        for (int i=0; i<argc ;i++) {
            [paramArray addObject:[[NSString alloc] initWithUTF8String:argv[i]]];
        }
        return (int) [[Whatever sharedInstance]manage:argc param:paramArray];
    }
    return 0;
}