xcode 如何将库包含到我的 Swift 项目中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42843054/
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 include a Library into my Swift Project?
提问by Blub
I want to read a .csv data in Swift, so I have informed me how to make this. Finally I got into this: https://github.com/Flinesoft/CSVImporterThe CSVImporter.
我想在 Swift 中读取 .csv 数据,所以我告诉了我如何制作。最后我进入了这个: https://github.com/Flinesoft/CSVImporter CSVImporter。
But it says: "You can of course also just include this framework manually into your project by downloading it".
但它说:“您当然也可以通过下载将此框架手动包含到您的项目中”。
That would be OK, but there are several folders and as I have never importer a library into Swift before, I don't know what to download and where I should include it into my project.
没关系,但是有几个文件夹,因为我以前从未将库导入 Swift,所以我不知道要下载什么以及应该将它包含在我的项目中的什么位置。
I hope, anybody can help me. Thanks.
我希望,任何人都可以帮助我。谢谢。
回答by Oleh Zayats
The best way to import third-party dependencies is via dependency managers: CocoaPods/Carthage/SPM, you will be able to update a lib without headache.
导入第三方依赖的最好方法是通过依赖管理器:CocoaPods/Carthage/SPM,你可以毫不费力地更新一个库。
1. CocoaPods[Offical Guide]
1. CocoaPods[官方指南]
This is by far the easiest way to go...
这是迄今为止最简单的方法......
Open terminal
打开终端
Install CocoaPods (type in terminal):
安装 CocoaPods(在终端中输入):
sudo gem install cocoapods
Sudo means "super user do", it will require your password. Enter when requested.
Sudo 的意思是“超级用户做”,它需要你的密码。要求时输入。
Next, you need to setup the cocoapods master repo. Type in terminal:
接下来,您需要设置 cocoapods 主存储库。输入终端:
pod setup --verbose // verbose option logs the setup progress
then create a pod file in your project directory (you can type "cd " and drag the project folder to the terminal window if your not comfortable with writing the path):
然后在你的项目目录中创建一个 pod 文件(如果你不习惯写路径,你可以输入“cd”并将项目文件夹拖到终端窗口):
cd User/Projects/YourProject // make way to your project dir
pod init
then inside the Podfile (find it in project directory) insert:
然后在 Podfile 中(在项目目录中找到它)插入:
platform :ios, '8.0'
use_frameworks!
target 'YouAppTarget' do
pod 'CSVImporter', '~> 1.7'
end
(Uncomment platform :ios, '8.0' Uncomment user_frameworks! if you're using Swift)
(取消注释平台 :ios, '8.0' 取消注释 user_frameworks!如果您使用的是 Swift)
Run (while in your project dir):
运行(在您的项目目录中):
pod install
Xcode .xcworkspacefile will be generated, thats it, you can open it and use the framework ;]
将生成Xcode .xcworkspace文件,就是这样,您可以打开它并使用框架;]
later on you can update lib with:
稍后您可以使用以下命令更新 lib:
pod update
pod update
2. Carthage[Offical Guide]
2.迦太基[官方指南]
Steps to install a framework via Carthage are pretty similar to CocoaPods, follow the 'Offical Guide' link to see what's the differences are. A downside about this dependency manager is that not all libs are available. Some are only available for CocoaPods, but majority of new ones are supporting Carthage. P.S. Here's a good articleon the differences.
通过 Carthage 安装框架的步骤与 CocoaPods 非常相似,请按照“官方指南”链接查看不同之处。此依赖项管理器的缺点是并非所有库都可用。有些只适用于 CocoaPods,但大多数新的都支持 Carthage。PS 这是一篇关于差异的好文章。
3. Swift Package Manager[Offical Overview/Guide]
3. Swift Package Manager[官方概述/指南]
SPM is a native dependency manager, it's cross-platform, officaly-supported by Apple, decentralized and open-source.
SPM 是一个原生的依赖管理器,它是跨平台的,由 Apple 官方支持,去中心化和开源。
It was planned as a replacement for CocoaPods and Carthage so I'd give it a try too.
它计划作为 CocoaPods 和 Carthage 的替代品,所以我也想尝试一下。
回答by user2773289
You can drag the CSVImporter.xcodeproj and drop it into your project, also include CSVImporter in the embedded binaries.
您可以将 CSVImporter.xcodeproj 拖放到您的项目中,也可以将 CSVImporter 包含在嵌入的二进制文件中。