Library? Static? Dynamic? Or Framework? Project inside another project

后端 未结 3 1912
一生所求
一生所求 2020-11-22 10:13

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 s

3条回答
  •  孤独总比滥情好
    2020-11-22 11:02

    Mach-O file format(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]

    Package is a directory which behavious itself as a file - opaque file. It is created for user experience to 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

    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 several types of bundle:

    • application bundle - Application target
    • framework bundle and versioned bundle as a subtype - Framework Target
    • loadable bundle(aka plug-in bundle) - Bundle target(UI Testing Bundle, Unit Testing Bundle)
    • others(dSYM[About] bundle)

    Application - .ipa, .app[About] - packaged application bundle - launchable program.

    Tests - packaged loadable bundle which is used to test a binary. Plug-in architecture allows us to add a new functionality(test cases) as a separate module into existing binary

    Libraries and Frameworks

    Martin Fowler on 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

    Library is a collection of Mach-O object files[check static or dynamic] compiled for one or more architectures.

    Static library - .a(aka static archive library, static linked shared library[doc]) - When you add it into your application the static linker during compilation time will 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

    From Xcode 9.0, Swift static library is supported.

    Dynamic library - .dylib(aka dynamic shared library, shared object, dynamically linked library[doc]) is dynamically linked with 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 system libraries are dynamic. The disadvantage is a slow launch time since all dynamic libraries should be copied and linked.

    [Static vs dynamic linking]

    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.

    Framework aka binary framework - .framework is a not packaged framework bundle(to allow developers to easily take a look at headers and resources) which contains a compiled static or dynamic library, header files and resources.

    Static framework contain a static library packaged with its resources.

    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)

    [Static vs dynamic framework]

    Embedded framework is a dynamic framework that lives in app’s sandbox. This type was created first of all for extension to share common code and resources. It's available when Deployment target is 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 recommended for 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.

    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.

    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.

    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]

    XCFramework was introduced by Xcode 11 and it is a bundle which includes multiple variants of a framework (different arches). It should replace a Universal Framework

    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.

    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]

    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]

    [Xcode Build System]
    [Xcode components]
    [Dynamic linker]

提交回复
热议问题