ios 图书馆?静止的?动态的?还是框架?另一个项目中的项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15331056/
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
Library? Static? Dynamic? Or Framework? Project inside another project
提问by pizzafilms
I have an existing iOS app and want to add a large chunk of code that I've been developing as another project just for ease of testing. The new chunk basically deals with saving an image to various sharing services, etc.. Because that sharing code needs a lot of testing and future updating, I was wondering what the best way to incorporate that code chunk into my existing app.
我有一个现有的 iOS 应用程序,想添加一大块代码,我一直在开发这些代码作为另一个项目,只是为了便于测试。新块主要处理将图像保存到各种共享服务等。因为共享代码需要大量测试和未来更新,我想知道将该代码块合并到我现有的应用程序中的最佳方法是什么。
I don't know if it should be a static library, dynamic library or a framework, and honestly, I'm not really sure what the difference is, or how I should go about it and get it set up in Xcode.
我不知道它应该是静态库、动态库还是框架,老实说,我不太确定有什么区别,或者我应该如何着手并在 Xcode 中设置它。
All I know is that I need/want to keep a separate testing and updating app for the sharing code and have the main app use it.
我所知道的是,我需要/想要为共享代码保留一个单独的测试和更新应用程序,并让主应用程序使用它。
回答by JRG-Developer
First, some general definitions (specific to iOS):
首先,一些通用定义(特定于 iOS):
Static library- a unit of code linked at compile time, which does not change.
静态库- 在编译时链接的代码单元,不会改变。
However, iOS static libraries are notallowed to contain images/assets (only code). You can get around this challenge by using a media bundlethough.
然而,iOS的静态库没有允许包含图像/资产(唯一的代码)。不过,您可以通过使用媒体包来解决这一挑战。
A better, more formal definition can be found on Wikipedia here.
一个更好、更正式的定义可以在维基百科上找到。
Dynamic library- a unit of code and/or assets linked at runtime that maychange.
动态库- 在运行时链接的可能会更改的代码和/或资产单元。
However, only Apple is allowed to create dynamic libraries for iOS . You're not allowed to create these, as this will get your app rejected. (See thisother SO post for confirmation and reasoning on such).
但是,只有 Apple 可以为 iOS 创建动态库。您不能创建这些,因为这会导致您的应用程序被拒绝。(有关此问题的确认和推理,请参阅此其他 SO 帖子)。
Software Framework- a compiled set of code that accomplishes a task... hence, you can actually have a static frameworkor a dynamic framework, which are typically just the compiled versions of the above.
软件框架- 完成任务的一组编译代码......因此,您实际上可以拥有静态框架或动态框架,它们通常只是上述框架的编译版本。
See the Wiki on Software Frameworkfor more details.
Hence on iOS, your only option is basically to use a static library or static framework (the main difference being that a static framework is distributed as a compiled .a
file most often, whereas a static library may simply be included as a subproject - you can see all of the code - which is compiled first and its resulting .a
file used as a dependency by the project).
因此,在 iOS 上,您唯一的选择基本上是使用静态库或静态框架(主要区别在于静态框架.a
最常作为编译文件分发,而静态库可能只是作为子项目包含 - 您可以看到所有代码 - 首先编译并将其结果.a
文件用作项目的依赖项)。
Now that we're clear(er) on these terms, setting up a static library and supporting media bundle for iOS isn't too difficult, and there are many tutorials on how to do such. I personally would recommend this one:
现在我们已经清楚(呃)这些条款,为 iOS 设置一个静态库和支持媒体包并不太困难,并且有很多关于如何做到这一点的教程。我个人会推荐这个:
https://github.com/jverkoey/iOS-Framework
https://github.com/jverkoey/iOS-Framework
This is a pretty straight-forward guide and doesn't have the disadvantage of dealing with "fake static libraries"... check it out for more info...
这是一个非常直接的指南,没有处理“假静态库”的缺点......查看更多信息......
Once you've created your static library, it's as easy as including it as a submodulewithin Git for use across different projects.
创建静态库后,就像将其作为子模块包含在 Git 中以便在不同项目中使用一样简单。
Good Luck.
祝你好运。
EDIT
编辑
Regarding a subprojectwithin a project, as far as I know, to get this to work/compile correctly, you essentially have to set up a compile chain where the subproject is compiled first, which creates a static framework .a
file that is used as a dependency by the project.
关于项目中的子项目,据我所知,要使其正常工作/编译,您基本上必须设置一个编译链,其中首先编译子项目,这会创建一个.a
用作依赖项的静态框架文件由项目。
Here's another useful tutorial which talks about this:
这是另一个有用的教程,它讨论了这个:
http://www.cocoanetics.com/2011/12/sub-projects-in-xcode/
http://www.cocoanetics.com/2011/12/sub-projects-in-xcode/
EDIT 2
编辑 2
As of iOS 8, Apple now permits developers to create dynamic frameworks! (Note: your app must have a minimum target of iOS 8 to include a dynamic framework... back porting isn't allowed.)
从 iOS 8 开始,Apple 现在允许开发人员创建动态框架!(注意:您的应用程序必须具有 iOS 8 的最低目标才能包含动态框架......不允许向后移植。)
This has been added as a new project template. In Xcode 6.1, this can be found at:
这已被添加为新的项目模板。在 Xcode 6.1 中,可以在以下位置找到:
New Project -> iOS -> Framework & Library -> Cocoa Touch Framework
回答by yoAlex5
Mach-O file format(Mach Object - .o
)
Mach-O 文件格式(Mach Object - .o
)
In iOS world every source file is converted into object files - ABI[About]Mach-O file[About]which will be packaged into a final executable bundle(e.g. application, framework...), file (e.g. library...) and it's behavior is determined by Mach-O type
[About]
在 iOS 世界中,每个源文件都被转换为目标文件 - ABI [About]Mach-O 文件[About]将被打包成最终的可执行包(例如应用程序、框架...)、文件(例如库...)它的行为由[About]决定Mach-O type
Package
is a directory which behavious itself as a file - opaque file
. It is created for user experienceto complicate making some changes into internal structure that can cause unpredictable program behaviour. Package is used in Document Package
or with a Bundle
. You can use Show Package Contents
in a Finder
Package
是一个目录,它本身作为一个文件 - opaque file
. 它是为用户体验而创建的,使对内部结构进行一些更改变得复杂,这可能会导致不可预测的程序行为。包用于Document Package
或与Bundle
. 您可以Show Package Contents
在 Finder 中使用
Bundle
is a directory with a specific structure to organize a binary(executable code) and resources for that code(e.g. images, nibs...).
Bundle contains Info.plist
[About]file. Bundle was created for developer experience. Also it can be packaged. There are three types of bundle:
Bundle
是一个具有特定结构的目录,用于组织二进制(可执行代码)和该代码的资源(例如图像、笔尖...)。捆绑包包含Info.plist
[关于]文件。Bundle 是为开发人员体验而创建的。也可以打包。捆绑包分为三种类型:
application bundle
-Application target
framework bundle
andversioned bundle
as a subtype -Framework Target
loadable bundle
(akaplug-in bundle
) -Bundle target
(UI Testing Bundle, Unit Testing Bundle)
application bundle
——Application target
framework bundle
并versioned bundle
作为一个子类型 -Framework Target
loadable bundle
(又名plug-in bundle
)-Bundle target
(UI 测试包,单元测试包)
Application
- .ipa
, .app
[About]- packaged
application bundle
- launchable program.
Application
- .ipa
, .app
[关于]- packaged
application bundle
- 可启动的程序。
Tests
- packaged
loadable bundle
which is used to test a binary. Plug-in architecture allows us do add a new functionality(test cases) as a separate module into existing binary
Tests
-packaged
loadable bundle
用于测试二进制文件。插件架构允许我们将新功能(测试用例)作为单独的模块添加到现有二进制文件中
Libraries and Frameworks
库和框架
Martin Fowler on InversionOfControl
Martin Fowler 谈 InversionOfControl
A Library is essentially a set of functions that you can call, these days usually organized into classes. Each call does some work and returns control to the client.
A Framework embodies some abstract design, with more behavior built in. In order to use it you need to insert your behavior into various places in the framework either by subclassing or by plugging in your own classes. The framework's code then calls your code at these points. The main control of the program is inverted, moved away from you to the framework. (Inversion of Control)
库本质上是一组可以调用的函数,如今通常组织成类。每个调用都会做一些工作并将控制权返回给客户端。
框架体现了一些抽象的设计,内置了更多的行为。为了使用它,你需要通过子类化或插入你自己的类,将你的行为插入到框架中的不同位置。然后框架的代码会在这些点调用您的代码。程序的主要控制被倒置,从你转移到框架。(控制反转)
Libraries and Frameworks on iOS
iOS 上的库和框架
Library
is a collection of Mach-O object files[check static or dynamic]compiled for one or more architectures.
Library
是为一个或多个体系结构编译的 Mach-O 目标文件[检查静态或动态]的集合。
Static library
- .a
(aka static archive library, static linked shared library[doc]) - When you add it into your application the static linkerduring compilation timewill merge the object files from the library and package them along with the application object files into one single executable file. The disadvantage is a big output file
Static library
- .a
(又名静态存档库,静态链接共享库[doc]) - 当您将其添加到应用程序中时,编译期间的静态链接器将合并库中的目标文件并将它们与应用程序目标文件一起打包成一个可执行文件文件。缺点是输出文件大
From Xcode 9.0, static Swift library is supported.
从 Xcode 9.0 开始,支持静态 Swift 库。
Dynamic library
- .dylib
(aka dynamic shared library, shared object, dynamically linked library[doc]) is dynamically linkedwith the app's executable at load or runtime, but not copied into it. On practice app's package will contain Frameworks folder with .dylib
file. All iOS and macOS systemlibraries are dynamic
. The disadvantage is a slow launch time since all dynamic libraries should be copied and linked.
Dynamic library
- .dylib
(又名动态共享库、共享对象、动态链接库[doc])在加载或运行时与应用程序的可执行文件动态链接,但不会复制到其中。在练习应用程序的包中将包含带有文件的Frameworks 文件夹。所有 iOS 和 macOS系统库都是. 缺点是启动时间很慢,因为所有动态库都应该被复制和链接。.dylib
dynamic
Text-based stub library
- .tbd
[About], it is a text stub of dynamic library
which is located on a target device. As a result you should not package a dynamic library into your bundle. It has a size effect.
Text-based stub library
- .tbd
[关于],它dynamic library
是位于目标设备上的文本存根。因此,您不应将动态库打包到您的包中。它具有尺寸效应。
Framework
- .framework
is a not packaged
framework bundle
- to allow developers to easily take a look at headers and resources and which contains a static or dynamic
library, header files and resources.
Framework
-.framework
是一个not packaged
framework bundle
- 允许开发人员轻松查看头文件和资源以及其中包含的static or dynamic
库、头文件和资源。
Static framework
contain a static library
packaged with its resources.
Static framework
包含一个static library
与其资源打包在一起的文件。
Dynamic framework
contains the dynamic library
and resources. In addition to that, dynamic framework can include different versions of the same dynamic library in a single bundle (versioned bundle
)
Dynamic framework
包含dynamic library
和 资源。除此之外,动态框架可以在单个包中包含同一动态库的不同版本(versioned bundle
)
Embedded framework
is a dynamic framework
that lives in app's sandbox. This type was created first of all for extensionto share common code and resources. It's available when Deployment target is iOS 8+.
Embedded framework
是一个dynamic framework
存在于应用程序沙箱中的。这种类型首先是为了扩展共享公共代码和资源而创建的。当部署目标为 iOS 8+ 时可用。
Umbrella framework
[Aggregate target]is a framework that contains other frameworks. It is not officially supported on iOS and that is why it is not recommendedfor developers to create them[Official doc]. In actuality it's a set of sub-frameworks(or Nested Frameworks). When you create a framework which has a dependency, a consumer (such as an app) is responsible for adding this dependency along with your framework into the project. As a developer, it's natural to try to find a way to transfer this duty from consumer to your's. As a result you think that Umbrella framework
is the rescue but usually it leads to a serious issues with managing versions and complexity of creating and supporting it.
Umbrella framework
[聚合目标]是一个包含其他框架的框架。它在 iOS 上不受官方支持,这就是为什么不建议开发人员创建它们[Official doc]。实际上它是一组子框架(或嵌套框架)。当您创建具有依赖项的框架时,使用者(例如应用程序)负责将此依赖项与您的框架一起添加到项目中。作为开发人员,很自然地尝试找到一种方法将这种责任从消费者转移到您的。因此,您认为这Umbrella framework
是一种拯救,但通常它会导致管理版本以及创建和支持它的复杂性方面出现严重问题。
Fake Framework
- is a result of specific operations under a static library
to create a bundle with .framework
extension that will behave yourself as a dynamic framework
. This technic was used when Xcode did not support creating a framework since did not have a framework template. One of realisation of a fake framework. With Xcode 6, Apple has added iOS framework support.
Fake Framework
- 是在 astatic library
下创建具有.framework
扩展名的包的特定操作的结果,该包将表现为dynamic framework
. 当 Xcode 不支持创建框架时使用此技术,因为它没有框架模板。假框架的实现之一。在 Xcode 6 中,Apple 添加了 iOS 框架支持。
Modular Framework
[About]- @import
it is a framework which contains a .modulemap
file inside. Module can contains submodules. The main advantage is that you save a build time with Modular Framework
.
Modular Framework
[关于]-@import
它是一个框架,里面包含一个.modulemap
文件。模块可以包含子模块。主要优点是您可以使用Modular Framework
.
Universal Library or Framework
(aka Fat) [lipo][Aggregate target]contains multiple architectures. For example your release build should support a some arch which you can regulate via Build Active Architecture Only
[ONLY_ACTIVE_ARCH]
Universal Library or Framework
(aka Fat) [lipo] [Aggregate target]包含多个架构。例如,您的发布版本应该支持一些架构,您可以通过[ONLY_ACTIVE_ARCH] 对其进行调节Build Active Architecture Only
Dependency
[About]You are able to use third party code as a part of your target. It allows you to reuse a code from a lot of sources like - another project, project in the same workspace, another target, library, framework etc.
Dependency
[关于]您可以使用第三方代码作为目标的一部分。它允许您重用来自许多来源的代码,例如 - 另一个项目、同一工作区中的项目、另一个目标、库、框架等。
How to build and use a Static Library:
如何构建和使用静态库:
- [Swift consumer -> Swift static library]
- [Swift consumer -> Objective-C static library]
- [Objective-C consumer -> Swift static library]
- [Objective-C consumer -> Objective-C static library]
- [Swift 消费者 -> Swift 静态库]
- [Swift 消费者 -> Objective-C 静态库]
- [Objective-C 消费者 -> Swift 静态库]
- [Objective-C 消费者 -> Objective-C 静态库]
How to build and use a Dynamic Framework[change to static]
如何构建和使用动态框架[更改为静态]
- [Swift consumer -> Swift dynamic framework]
- [Swift consumer -> Objective-C dynamic framework]
- [Objective-C consumer -> Swift dynamic framework]
- [Objective-C consumer -> Objective-C dynamic framework]
- [Swift 消费者 -> Swift 动态框架]
- [Swift 消费者 -> Objective-C 动态框架]
- 【Objective-C消费者->Swift动态框架】
- 【Objective-C消费者->Objective-C动态框架】
回答by Timur Kuchkarov
You can also create .podspec file for CocoaPods( http://guides.cocoapods.org/making/private-cocoapods.html#1.-create-a-private-spec-repo) and use it like any other pod with the only difference that it's your private pod and is not visible to outside world(I'm not sure what will happen if your pod should create CoreData model, but that's not the case, as I understand).
您还可以为 CocoaPods 创建 .podspec 文件(http://guides.cocoapods.org/making/private-cocoapods.html#1.-create-a-private-spec-repo)并像使用其他任何带有唯一的区别是它是您的私人 pod,对外界不可见(我不确定如果您的 pod 应该创建 CoreData 模型会发生什么,但据我了解,情况并非如此)。