Swift,iOS 8+框架 [英] Swift, iOS 8+ Framework

查看:113
本文介绍了Swift,iOS 8+框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个共享的 Swift 库(又称为Cocoa Touch Framework),以在我的两个iOS应用程序之间共享.我不是专职的iOS开发人员,所以我对XCode等的了解还很多.

I want to create a shared Swift library a.k.a. Cocoa Touch Framework to share between two of my iOS applications. I'm not a full-time iOS developer, so I have many gaps in my knowledge of XCode etc.

我发现这篇文章创建您的第一个iOS框架,并认为我家里免费:-).可悲的是,我无法使其正常运行,主要是因为我不知道我要做什么.

I found this article Creating your first iOS Framework and thought I was home free :-). Sadly, I can't get it working, mostly because I don't know what I'm trying to do.

我只是想在两个应用程序之间创建一个共享的Swift代码的(私有)框架(库).我想在框架中使用CocoaPods引入其他框架,例如Alamofire.我无意将此框架公开.

I simply want to create a (private) framework (library) of shared Swift code between my two applications. I'd like to use CocoaPods in the framework to bring in other frameworks such as Alamofire. I have no intention of making this framework public.

我几乎不明白为什么要使用Carthage(相对于CocoaPods)作为管理框架并将其导入使用的应用程序的机制.但是,该示例将迦太基和CocoaPods混合在一起,并设置了我不想要的Podspec,这使我感到困惑.

I barely understand why one would use Carthage (vs. CocoaPods) as the mechanism to manage the framework and import it into the consuming apps. But, the example mixes Carthage and CocoaPods and sets up a Podspec which I don't think I want and confuses me.

在本文中,一般而言,子项目的概念以及将Carthage下载的框架拖到框架中,然后将共享框架本身导入"到使用中的应用程序中,这一切都使我感到困惑.我想知道为什么要这么做以及它的后果.

In the article, the notion of sub-projects in general, and dragging the Carthage downloaded frameworks into the framework and "importing" the shared framework itself in the consuming apps all confuses the heck out of me. I want to know why I'm doing that and what the ramifications of it are.

那么,我会以错误的方式处理吗?有人在两个相关的应用程序之间私下共享 Swift 代码和其他工件的方法简单吗?

So, am I going about this the wrong way? Does anyone have a brain dead simple recipe for sharing Swift code and other artifacts privately between two related applications?

有人可以解释该理论吗?

当我考虑复制代码时,我只有很少的时间来处理我的应用程序,而且我的肉也爬行了,所以我浪费了无数的时间来尝试该简单指南,而浪费了很多时间来尝试遵循我所发现的指南-所有这些令人失望的结局.

I get very little time to work on my apps and my flesh crawls when I think about duplicating code, so I've wasted countless hours trying to find that simple guide and more hours attempting to follow those I have found - all to disappointing ends.

任何帮助将不胜感激.

谢谢, 彼得...

推荐答案

我建议您使用迦太基,因为它更简单并且不会对您的项目文件施加任何约束.我从未使用过CocoaPods,无法为您提供帮助,我将指导您完成迦太基的必要步骤:

I suggest you to use Carthage, because it's much simpler and doesn't impose any constraints on your project file. I cannot help you with CocoaPods as I have never used it, I'll guide you through the steps required with Carthage:

  1. 如果尚未安装,请安装 Homebrew ,它是OSX上最常用的软件包管理器.命令:

  1. If you haven't already, install Homebrew, it's the most commonly used package manager on OSX. Command:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

  • 安装迦太基.命令:

  • Install Carthage. Command:

    brew update && brew install carthage
    

  • 使用iOS Cocoa Touch Framework模板创建一个新项目.保存时,选中创建Git存储库".

  • Create a new Project, using the iOS Cocoa Touch Framework template. When saving, check "Create Git repository".

    .gitignore文件添加到您的项目文件夹.如果您在项目目录中,则可以执行以下操作:

    Add a .gitignore file to your project folder. If you're in the project directory, you can do this:

    curl https://raw.githubusercontent.com/github/gitignore/master/Swift.gitignore > .gitignore
    

  • 将所需的代码添加到项目中,请记住,只有public声明在框架外部可用.

  • Add the code you want to the project, remember that only public declarations are available outside the framework.

    编辑方案(快捷键⌘<)并检查Shared.这是迦太基工作所必需的.

    Edit the scheme (Shortcut ⌘<) and check Shared. This is required for Carthage to work.

    提交更改(快捷方式⌥⌘C),检查每个文件(右键单击>全部检查),然后输入提交消息(例如第一版")

    Commit the changes (Shortcut ⌥⌘C), check every file (Rightclick > Check All) and enter a commit message (e.g. "First version")

    标记版本(Carthage要求):命令(在项目目录中):

    Tag the release (required by Carthage): Command (In the project directory):

    git tag 0.1 -m "Version 0.1"
    

  • 运行以下命令以通过迦太基验证并构建项目:

  • Run the following command to verify and build the project via Carthage:

    carthage build --no-skip-current
    

  • 您的框架应该现在就可以与迦太基一起使用.您可以在此处中执行以下步骤:将框架添加到您的iOS项目中:

    Your framework should be set up to work with Carthage now. You can follow the steps here to add the framework to your iOS project:

    1. Cartfile 添加到您的项目(简称为名为Carthage的文件(无扩展名)),则可以为此使用Xcode.该文件应包含以下内容:

    1. Add a Cartfile to your project (simply a file named Carthage (no extension)), you can use Xcode for this. The file should contain this:

    git "file:///path/to/your/framework/project/directory"
    github "Alamofire/Alamofire"
    # Other frameworks
    

  • 运行

  • Run

    carthage update --platform iOS
    

  • 打开Carthage/Build/iOS文件夹(在Finder中),然后将每个.framework文件拖放到项目设置(常规"选项卡)中的Linked Frameworks and Libraries部分.

  • Open the Carthage/Build/iOS folder (in Finder) and drag-and-drop every .framework file to the Linked Frameworks and Libraries section in the project settings (General tab).

    添加一个新的Run Script阶段(项目设置">构建阶段">"+"按钮).把线

    Add a new Run Script phase (Project Settings > Build Phases > "+" button). Put the line

    /usr/local/bin/carthage copy-frameworks
    

    并将每个框架添加到输入文件"部分:

    and add every framework to the "Input Files" section:

    $(SRCROOT)/Carthage/Build/iOS/MyFramework.framework
    $(SRCROOT)/Carthage/Build/iOS/Alamofire.framework
    

  • 现在这应该可以工作,如果您被困在某个地方,请告诉我.

    Now this should work, let me know if you're stuck somewhere.

    对库进行更改时,请执行以下操作:

    When you make changes to the library, you do this:

    1. 提交
    2. git tag 0.2 -m "Version 0.2"

    在使用该框架的项目中,只需运行carthage update --platform iOS,就无需执行其他操作.

    In your project that's using the framework, just run carthage update --platform iOS, nothing else to do.

    如果您的框架还需要使用Alamofire,则它需要与其他项目相同的Cartfile设置.运行carthage update时,迦太基将解析每个框架所需的所有子帧工作及其子帧工作,等等.

    If your Framework needs to use Alamofire as well, it needs the same Cartfile setup as the other project. When running carthage update, Carthage will resolve all subframeworks needed for every framework, and their subframeworks, etc.

    例如,可能存在冲突.一个Cartfile想要"Alamofire" ~> "1.0",另一个"Alamofire" ~> "2.0",但这很少发生.

    There can be conflicts if e.g. one Cartfile wants "Alamofire" ~> "1.0" and the other "Alamofire" ~> "2.0", but that rarely happens.

    这篇关于Swift,iOS 8+框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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