xcode 如何将 swift 库项目编译成 .so 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24077215/
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
How to compile swift library project into .so file?
提问by spierce7
I'm new to XCode and Objective C / Swift. I'm trying to make a Swift library project that doesn't depend on cocoa or anything. So it's just Swift files. What type of project should I be creating (I'm assuming there are multiple ways of doing this? How do I need to configure the project so that it builds to a .so file? And finally, how do I build it to a .so file?
我是 XCode 和 Objective C/Swift 的新手。我正在尝试制作一个不依赖于可可或任何东西的 Swift 库项目。所以它只是 Swift 文件。我应该创建什么类型的项目(我假设有多种方法可以做到这一点?我如何配置项目以使其构建为 .so 文件?最后,我如何将其构建为 .so 文件?所以文件?
I'm using Xcode6-beta.
我正在使用 Xcode6-beta。
回答by melbyruarus
You can create a Swift library for iOS by creating a new Xcode Project and selecting iOS/OS X -> Framework & Library -> Cocoa Touch Framework and then setting the language to Swift on the next screen.
您可以通过创建一个新的 Xcode 项目并选择 iOS/OS X -> Framework & Library -> Cocoa Touch Framework,然后在下一个屏幕上将语言设置为 Swift 来为 iOS 创建一个 Swift 库。
Once this project has been created you are not required to link against the Cocoa frameworks, and can remove any references to them if needed.
创建此项目后,您不需要链接 Cocoa 框架,并且可以根据需要删除对它们的任何引用。
Upon compiling this project Xcode will create a .framework folder in your build directory which contains a dynamic library which you should then be able to link against. Note however that I haven't had any success myself yet at linking directly against this file, even though it appears to be a standard dynamic library.
编译此项目后,Xcode 将在您的构建目录中创建一个 .framework 文件夹,其中包含一个动态库,然后您应该能够链接到该库。但是请注意,我自己在直接链接到这个文件方面还没有取得任何成功,即使它看起来是一个标准的动态库。
In terms of using this shared object file on Android I believe you will be out of luck, as the Swift language (much like Objective-C) requires a runtime library which is simply not available on other platforms at this time.
就在 Android 上使用这个共享对象文件而言,我相信你会不走运,因为 Swift 语言(很像 Objective-C)需要一个运行时库,目前在其他平台上根本不可用。
回答by Snymax
... I think you are limiting your train of thought a bit or expecting leaps and bounds to quickly swift is ready to work with objective c and cocoa so you aren't going to be able completely avoid cocoa however you wont have to write any of it the compiler will do that for you. but you will still have to use cocoa frameworks or modules like foundation( contains many specialized objects for program development), UIKit, CoreData however these don't require you to write the cocoa. and if Im not mistaken the .so file is made on compile so you never actually touch that file but rather is supporting files .swift
...我认为您有点限制了您的思路或期望快速跨越式发展 swift 已准备好使用目标 c 和可可,因此您将无法完全避免可可,但您不必编写任何其中编译器会为你做这件事。但是你仍然需要使用 cocoa 框架或模块,比如 Foundation(包含许多用于程序开发的专门对象)、UIKit、CoreData 但是这些不需要你编写可可。如果我没记错的话,.so 文件是在编译时生成的,所以您实际上从未接触过该文件,而是支持文件 .swift
to do it
去做吧
if your looking to do everything programmatically or if you want to xib or storyboards just make a new project I prefer an empty project.(set programming language to swift and decide if you want core data or not finish the project wizard thing and bam you have a YourAppDelegate.swift you can make another .swift file or a storyboard and hopefully you know what to do from there. Swift book is available on ibooks for free its nicely written
如果您希望以编程方式完成所有工作,或者如果您想创建一个新项目,我更喜欢一个空项目。(将编程语言设置为 swift 并决定是否需要核心数据或不完成项目向导的事情,然后你有一个 YourAppDelegate.swift 你可以制作另一个 .swift 文件或故事板,希望你知道从那里做什么。Swift 书在 ibooks 上免费提供,它写得很好
although it will be quite a while before cocoa and swift are completely separate... if ever
虽然在可可和斯威夫特完全分开之前还需要很长时间......如果有的话