This is the 25th day of my participation in the August Genwen Challenge.More challenges in August


My column

  1. Explore the underlying principles of iOS
  2. Summary of iOS underlying principles of exploration

What is a library?

Libraries are ways of sharing program code. A library is essentially a binary format of executable code that can be loaded into memory for execution. Libraries are divided into static libraries and dynamic libraries. Static libraries in iOS come in. A and. Framework forms; Dylib and.framework, and then the.dylib dynamic library was replaced by Apple with.tbd.

Static library: complete copy to executable file when connected, multiple use of multiple redundant copies. Dynamic library: link does not copy, when the program runs, by the system dynamic loading into memory, for the program to call, the system only loaded once, multiple programs common, saving memory. IOS will only allow use of the system dynamic library for now.

Static libraries and dynamic libraries are relative to compile time and run time: static libraries are connected to the object code when the program is compiled, and there is no need to change static libraries when the program is run. Dynamic libraries are not connected to the object code at compile time, but are only loaded when the program is running, because the dynamic libraries are needed during the program’s runtime. Summary: When the same static library is used in different programs, each program must be imported once, and packaged into a package to form a program. Dynamic libraries in different programs are not packed into the package, and are linked to load only when the program is running (such as UIKit, Foundation, etc.), so the program size is much smaller, but Apple does not allow the use of its own dynamic library, otherwise it cannot pass the audit.

Why is the Framework both a static and a dynamic library?

The. Framework of the system is a dynamic library, which we build ourselves. The framework is a static library.

What is the difference between.a and. Framework?

A is a pure binary file. The. Framework has resource files in addition to binary files. A files cannot be used directly, but at least a. H file must be used together. Framework files can be used directly. .a + .h + sourceFile = .framework

Framework is recommended

Why use a static library?

Easy to share code, easy to use. Achieve iOS program modularization. You can modularize a fixed business into a static library. Share your code base with others, but don’t want others to see the implementation of your code, the need to develop third party SDKS.

A few things to note when creating a static library:

  • 1. Pay attention to understanding: Whether the. A static library or. Framework static library, we need the form of binary files +. H + other resource files, the difference is that. A is a binary file, we need to match.

  • 2, image resources processing, two kinds of static library, usually put the image files in a separate. Bundle file, usually the name of the. The.bundle file is easy to do. Create a new folder, rename it.bundle, right-click the package and add images to it.

  • 3. Categories are often used in our actual development projects. There is no problem with classifying a category into a static library, but in this static library project, there is a runtime error (selector not recognized) when calling a method in a category. The workaround is to set other Linker Flags to -objc in the static library project.

  • 4, if a static library is very complex, needs to be exposed. H is more, can be in internal creates a static library. H file (generally the. H file name and the name of the static library), and then put all need to be exposed. H file is focused on this. H file, and those that need to be exposed. H don’t need to be exposed, You just have to expose the dot h.

Create a static library

  • The first step is new construction. Generally use the project name to use the name of the library, for example, here we use Jingtaiku to create a static library, our project name is jingtaiku, created. A static library is Jingtaiku.

  • Step 2 Import the resource file to be packaged and import the. H file that requires external access to the recommended. H file.

  • The third step, the real machine, the simulator compile. Can generate. A file (must first use a real machine, otherwise, can not generate).

  • Step 4. The. A file generated by Xcode is not exported by default. You need to add them yourself.

  • Step 5. Export your own static library configuration

Note: If you change Build Configuration to Release instead of Build Configuration in step 5, the packaged static libraries will be stored in the debug-iphoneOS and debug-iphonesimulator folders. We usually use Release mode, because the program will eventually be released, so static libraries are also used in Release mode.

  • Step 6: Synthesize the architecture of the simulator [default: the simulator will generate only one corresponding architecture]

If set to YES in step 6, the compiled.A file library contains only the architecture of the current device.

  • Step 7. Merge architecture [real machine and simulator]

Static library 2.a (path) -output static library. A (you’d better CD to the project directory, so that the exported files are in your project directory)

  • Step 8: The problem of resource packs

1. Static library resources should be stored in a. Bundle folder to avoid overwriting files with the same name as local files, which may cause errors in loading resource files. 2, Static library packaging does not package resource files -> need to manually drag into