iOS practical skills extension-making and simple use of static libraries

Source: Internet
Author: User

This paper is about the concept description of static library, how to make and use the simple debugging, the description of different versions and the use of the scene.

1. Introduction to Libraries:

The library can be divided into 2 different types

    1. Open Source Library
      1. Open source code, can see concrete implementation
      2. Like Sdwebimage, afnetworking.
    2. Closed Source Library
      1. Unfair open source code, is a compiled binary file, do not see the specific implementation
      2. Mainly divided into: static library, dynamic library

2. Static Library Features:. a+.h;

Why create a closed source Library

    1. Domestic enterprises, mastering the core technology, but also hope that more programmers to use its technology, so the use of "closed source" way to develop using such as: Baidu Map, Friends League, Jpush, etc.
      1. In enterprise development, some core technology or common framework, for security and stability considerations, will also provide a static library for programmers to use
        1. Static libraries and dynamic libraries
        2. The existence form of static library and dynamic library
          1. Static libraries:. A and. Framework
          2. Dynamic libraries:. Dylib (before Xcode7.0). Tbd[xcode7 later] and. Framework
      2. The difference between static and dynamic libraries in use
        1. Static libraries: When linked, the static library is completely copied to the executable file, and multiple copies of the redundant copy are used by several applications (shown in the image on the left)
        2. Dynamic Library: Link is not copied, the program is dynamically loaded into memory by the system, for program calls, the system is only loaded once, multiple programs shared, save memory (shown)

3. (can be ignored, because the current less use of xcode6) need to note that: If you use a self-made dynamic library, Xcode6 can not be uploaded to Appstore,xcode6 after the upload AppStore.

3. Simple production of Static library

1> New Tools Project


2> create a class method that implements a simple addition

    • Tools.h file
  + (double) sumNum1:(double) x andNum2:(double) y;
    • TOOLS.M file
    + (double)sumNum1:(double)x andNum2:(double)y     {         return  x + y;     }


3> compiling
# Note that when compiling a static library, you need to compile two versions of the real machine (arm) | Simulator (i386)

      • View the file navigation bar->PRODUCT->LIBTOOLS.A file if the 红色 . A file is not generated, if it is a 黑色 . A file is generated.
      • Simply compiling the emulator does not generate. A, it needs to be compiled under a real machine to generate. A Files
      • Select. a file, right-click->showfinder, generate two. A files, one for the real machine, one for the simulator


4> test-generated static libraries

Create a new toolstest iOS project

    1. Drag the. A +. h of the generated emulator to the project to use
    2. Import Tools.h files in VIEWCONTROLLER.M
    3. Use the Call class Method + (Double) SumNum1: (double) x andNum2: (double) y;
   - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{   double rs = [Tools sumNum1:10 andNum2:20]; NSLog(@"%lf",rs);}

Output results: 30

4. Use of resource bundles in a static library

Problem: Some third-party libraries use some image material, such as the company logo. However, because Xcode imports all footage files into Mainbundle by default at compile time, in order to avoid conflicts with programs that use static libraries. In a static library, if you want to use a picture material, you use the bundle method.
Steps:
1> build bundles and add pictures to them
2> create a class method that returns a picture
3> compiling
4> callers need to be imported if they need to. h +. A + Xxx.bundle

5. Merging of static libraries (because the emulator is not the same as the real machine, so two platforms are not universal)
Adjust the simulator to iphone5, compile the error


# Type of Detection. A
$ lipo-info LIBCZTOOLS.A
# merge. A
Lipo-create DEBUG-IPHONEOS/LIBTOOLS.A debug-iphonesimulator/libtools.a-output LIBTOOLS.A


How to let build. A file support i386?

Merging static libraries

    1. A static library can be merged with a terminal command: lipo -create 静态库1.a 静态库2.a -output 新静态库的名称.a

2. View the generated static library:

Test the generated static library

      1. Replace the original. A file in the test project with a new. a file
      2. Run the test project separately under 4s,6s and real machines


# Benefits of merging. A: Can be debugged on the real machine during the development process, or it can be debugged on the simulator
# The downside of merge. A: If the static library is too large, the merge package will be very large, so many third-party static libraries. A is a version-sensitive
# Be sure to note the version in the future when using. A

6. How to build a release static library
    1. By default, the generated version is a 调试版 static library, how to generate 发布版 static?
    2. Steps:

      1. Edit Scheme

      2. Configuration runs for publishing

    3. Compile the static library (both on the simulation and on the real machine) to generate a release static library
      1. Release version of the true Machine edition Release-iphoneos
      2. Release version of emulator version Release-iphonesimulator

Add:

01-Schema parsing
Undefined Symbols for architecture i386: undefined symbol in i386 schema
Now the static library is not available on the i386 schema

Simulator:
Iphone4s-->iphone5:i386
Iphone5s-->iphone6plus:x86_64

Real machine:
Iphone3gs-->iphone4s:armv7
iphone5-->iphone5c:armv7s
Iphone5s-->iphone6splus:arm64

See which schemas are supported by the static library with the following command: Lipo-info Static Library. A
You can merge a static library with the following commands:
Lipo-create Static library 1.a Static library 2.a-output The name of the new Static library. A

02-Types of static libraries
================================================================================

Version of the static library file (4 types)
1. Real Machine-debug version
2. Real Machine-release version
3. Simulator-debug Version
4. Simulator-release Version

Debug version (Debug version) VS release version (release version)
--------------------------------------------------------------------------------
-The debug version will contain complete symbolic information for easy debugging
-The debug version does not optimize the code

-The release version does not contain complete symbolic information
-The execution code of the release version is optimized
-The release version size is slightly smaller than the debug version
-The release will be faster in terms of execution speed, but does not imply a significant improvement

iOS practical skills extension-making and simple use of static libraries

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.