Preface:

This article is my work in the process of developing the Framework of the pit through the detour of the summary, this tutorial is suitable for Swift and OC, attached at the end of the article may encounter problems and solutions, I hope to give you a little help development partners.

This paper mainly talks about the following contents:

  1. The difference between dynamic and static libraries
  2. How do I create the Framework for Swift
  3. How to debug the Framework, two ways
  4. About the CPU architecture of the Framework and how to compose the Framework architecture to support both real machines and emulators, two ways
  5. Potholes that may be encountered in the development framework and their solutions

Start:

In our development, the most inseparable is the Framework such as UIKit. Framework, so it should be familiar with the Framework, then in the development of their own module code into Framework, the scenario is as follows:

  1. Convenient for others to use our own modules
  2. For use by third parties who do not want others to see their internal implementation logic
  3. Modularity improves code reuse

The difference between dynamic and static libraries

Static library:

When linking, it copies the executable file in its entirety. When used multiple times, there are multiple redundant copies.

The dynamic library:

Link is not copied, the program is dynamically loaded into memory by the system when it is running, for the program to call, the system is only loaded once, multiple programs share, saving memory.

The difference between:

Static libraries and dynamic libraries are relative to compile time and run time: static libraries are linked to the object code when the program is compiled, so the program does not need to change the static library when it is run. Dynamic libraries are not linked to the object code when the program is compiled, but are only loaded when the program is running, because dynamic libraries are needed during the program’s runtime.

Conclusion:

When the same static library is used in different programs, it must be imported once in each program and packaged into a single program. The dynamic library is not packaged in different programs. It is linked to load only when the program is running and used (such as the framework of the system, such as UIKit and Foundation, etc.), so the program size is much smaller. However, Apple does not allow the use of its own dynamic library, otherwise it cannot pass the audit.

Here is a step-by-step demonstration of how to make a framework, and the pitfalls you might encounter in making your own framework, and how to resolve them. As a demonstration to do a relatively simple array disorderly framework

Create a New Framework

  1. File -> New -> Progect-> Select iOS -> Cocoa Touch Framework

  1. Go to Next -> Protect Name for ArrayDisorderSDK Launguage and select Swift and click Next

  1. Create a new File in the Framework: You can select Cocoa Touch Class or Swift File as your source

I’m going to call it ArrayDisorder, and I’m going to create it

  1. In the ArrayDisorder class, write code for an array that is out of order

open class ArrayDisorder: NSObject {
    
    open func disorder (orders:Array<Any>) -> Array<Any> {
        var temp = orders
        var count = Int(temp.count)
        while count > 0 {
            let index = Int(arc4random_uniform(UInt32(Int32(count))))
            let last =  Int(count-1)
            temp.swapAt(index, last)
            count -= 1
        }
        return temp
    }
}

Copy the code

Note: You may notice the open keyword in front of the class or disorder method, but here are the differences between this and the OC Framework

  • Oc makes the framework and generates a class with the same name as the framework
/ /! Project version numberforArrayDisorderSDK. FOUNDATION_EXPORT double ArrayDisorderSDKVersionNumber; / /! Project version stringfor ArrayDisorderSDK.
FOUNDATION_EXPORT const unsigned char ArrayDisorderSDKVersionString[];

// In this header, you should import all the public headers of your framework using statements like #import <ArrayDisorderSDK/PublicHeader.h>The above this sentence annotations made it very clear If you want to let others find you outside the framework of the class you must like < ArrayDisorderSDK/PublicHeader. H > this format as your class to import this fileCopy the code

The last sentence

// In this header, you should import all the public headers of your framework using statements like #import <ArrayDisorderSDK/PublicHeader.h>

Copy the code

Made it very clear if you want to let others find you outside in the framework of the class you must like < ArrayDisorderSDK/PublicHeader. H > this format as your class to import this file, the enlisted into the SDK The generated header file then shows the classes and methods that you open to the outside world

Creating the Swift Framework also generates such a file but you do not need to import the header file like this. Just write Open or Public in front of the class name and method name that you want to expose to the user and when compiled into the Framework it will generate a file called “the name of your Framework -swift.h” and in this file you can see that the table before the class name has Public or Class for the Open keyword.

When do YOU use Open and when do you use Public?

  1. Open is accessible outside of scope, inherits, and Open class members decorated with the Open keyword are accessible and overridden outside scope.

  2. Pubic is accessible outside of scope, but there are no subclasses outside scope. Public class members are accessible, but not overridden outside of scope.

Configure your Framework

  1. Configure dynamic or static libraries

Go to framework target build settings-linking -> Mach-o Type -> Static Library or Dynamic Library

  1. Development Target

Set it to support iOS 8 in this Demo

  1. Also configure the Build Configuration to run the compiled version as Release. If it is not release, running in a Release environment will cause an error

At this point, the basic Framework is basically complete, Command + B

Note: If Device is selected as emulator when compiling, it will generate x86 architecture, only emulator is supported, the compiled Framework will not work on the real machine, if Device does not select or select the real machine you are connected to, it will compile to ARM64 It can run on a real machine but not on an emulator. If you want to create a framework that runs on both the simulator and the real machine, you need to combine the two architectures, which will be discussed in more detail later.==

Click on the generated Framework and Show In Finder

As shown in figure in, generates ArrayDisorderSDK. The framework then this file can be used directly to others.

Debugging Framework

We’re used to debugging as we write code, so there are two ways to debug our Framework when we’re building it

The first (not recommended)

Create a new Progect named testArrayDisorderDemo as follows

Drag the compiled Framework directly into testArrayDisorderDemo

Then the import ArrayDisorderSDK

Put the following code in ViewDidLoad

  letDisOrder = ArrayDisorder() // A class that encapsulates the disOrder function // Disorder. disOrder (orders: [1,2,3,4,5,6,7,8,9]) calls the classes that ArrayDisorder has publishedprint(disOrder. DisOrder (the orders:,2,3,4,5,6,7,8,9 [1])) / / output: [8, 6, 2, 5, 3, 4, 7, 9, 1)Copy the code

At this point, our simple framework is complete.

However, this method of debugging the framework is inefficient. We need to delete the original framework from testArrayDisorderDemo and import it again every time we debug it. So here is a more convenient and efficient method.

Second, create a dependency project (recommended)

TestArrayDisorderDemo is also a test Framework project

As is shown in

Then drag the ArrayDisorderSDK into the testArrayDisorderDemo file as shown

Then open up testArrayDisorderDemo

– > General – > Embedded Binaries – > click on the plus sign – > add Other – > select ArrayDisorderSDK. Xcodeproj

The following figure

)]

If you go to Target, you’ll see that there’s already a Taeget that’s testArrayDisorderDemo and an ArrayDisorderSDK

If you check testArrayDisorderDemo you’re running the Demo and if you check ArrayDisorderSDK you’re building the SDK

And you’ll notice that you can import the ArrayDisorderSDK directly into the ViewController of testArrayDisorderDemo instead of deleting it and dragging it in. And most importantly, when testArrayDisorderDemo Tagret that you’ve selected is running and when the program goes into the framework you can debug it by breaking points and so on. It will be very efficient.

Note: To test your changes when you modify code in the framework, you must first compile the target of your framework, as shown in this article. If you don’t compile the target of the ArrayDisorderSDK, you test you The Demo of the framework uses the original framework instead of the modified one

About the Framework’s CPU architecture

The CPU architecture varies in different models as follows:

Arm7: Used on the oldest iOS7 supported devices

Arm7s: Used on iPhone 5 and iPhone 5C

Arm64: On the iPhone 5S’s 64-bit ARM processor

I386: Used on 32-bit emulators

X86_64: Used on 64-bit emulators

Of course, a Framework does not need all support, can be based on the need.

As mentioned above, If Device is selected as emulator, it will generate x86 architecture, only emulator is supported, the compiled Framework will not work on the real machine, if Device does not select or select the real machine you are connected to the computer, it will compile arm64 and ARM7 You can run it on a real machine but not on an emulator, so if you’re making a framework that runs on an emulator and on a real machine, you need to combine those two architectures, and you need to make a composite architecture.

First of all, how do you look at the architecture of a framework

Use the lipo-info command

Note: -info – and info have no Spaces,info has a space after it

As is shown in

Drag the ArrayDisorderSDK file into the console and press Enter to output the CPU architecture of ArrayDisorderSDK such as ARMV7 arm64

This is supported by the real machine, if the support simulator does not work, need to recompile:

Can also be seen in the above selected ArrayDisorderSDK. The framework show in the Finder, you will find in a layer has four folder (up to four if you only really under relesae on compiling Only one folder), mainly divides into the Debug and Debug-iphoneos/ debug-iphonesimulator Debug-iphoneOS/debug-iphonesimulator debug-iphoneOS/debug-iphonesimulator Relesase under the real machine and emulator

So what we need to do is to combine the two architectures under release-iphoneOS/release-iphone ulator

Schema consolidation:

The first method (not recommended)

Add the name ArrayDisorderSDK to your framework with the lip-create aa bb-output cc command

Aa: it means the Release – ArrayDisorderSDK iphoneos folder. The path of the framework/ArrayDisorderSDK bb: The Release – iphonesimulator folder ArrayDisorderSDK. The framework/ArrayDisorderSDK

Cc: The generated filename is the same name as your framework, such as ArrayDisorderSDK

The generated output ArrayDisorderSDK is needed to Release – iphoneos folder ArrayDisorderSDK. ArrayDisorderSDK replace of the framework.

Note :-create and -output have no space before create and output, but there is a space after create and output

The above may not be easy to understand, so the following demonstration, simplification: as shown

In short, this method is too troublesome, prone to error, here is another relatively simple.

Second :(recommended)

  1. The selected ArrayDisorderSDK. Xcodeproj for UniversalArrayDisorder target to create a new name

  1. Select UniversalArrayDisorder –> Build Phases –> select plus –> select New Run Script Phase

Then in the script address

Download a file named universal-framework.sh and copy the file contents as shown in the figure below

Note: ${PROJECT_NAME} in universal-framework.sh replaces this with the name of your own framework

After the UniversalArrayDisorder target is successfully compiled, it will automatically open a folder that generates the Framework, and then view the schema information. You will find that both the real machine and the simulator are supported.

You may come across a pit

1 The framework cannot be found

Error message:

No such module xxx
Copy the code

Solution:

The Framework Search Path is set manually. This Path is automatically generated when you drag custom frameworks to projects that reference your Framework, so the simple solution is to delete it Note: Make sure that the framework and the target project are in the same folder when you drag them, so there will be no problems

2 Class not found

Error message:

'xxxx' is unavailable: cannot find Swift declaration forThis class XXXX represents the name of a class in the frameworkCopy the code

Solution:

Framework architecture error: If your framework needs to run on emulators then your framework must include x86 and if it needs to run on real machines then it must include arm64

Dynamic library and static library problems

Error message:

Reason: image not found
 Message from debugger: Terminated due to signal 6
Copy the code

Solution:

Embedded Binaries does not add your framework name. Framework -> Add

Write at the end:

References:

  1. :https://medium.com/flawless-app-stories/getting-started-with-reusable-frameworks-for-ios-development-f00d74827d11

  2. :https://medium.com/captain-ios-experts/develop-a-swift-framework-1c7fdda27bf1

  3. :https://www.raywenderlich.com/65964/create-a-framework-for-ios

This article is a summary of the problems ENCOUNTERED in my development. The general trend is how to create my own framework more conveniently and efficiently. There are few similarities and differences between OC and Swift to create framework. Due to busy work, the level is limited, it is hard to avoid incomplete, or inappropriate place, but also please see this article’s friends give advice. Or if you don’t understand this article, you would like to communicate in the comments section. If this article solves your problem, please give it a thumbs up.

The Demo address Welcome to reprint, reprint please indicate the source: https://juejin.cn/post/6844903545620152327