- Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
1. Componentized introduction
As a project gets bigger and bigger, with more and more developers, it can run into a lot of problems.
- Service modules are not clearly divided, and the coupling degree between modules is very high, which makes it difficult to maintain.
- All module code is written in a project to test a module or function, and the entire project needs to be compiled and run.
To solve these problems, componentization is needed. Componentization functions as follows:
- Decoupling between modules
- Module reuse
- Improve team collaboration and development efficiency
- Unit testing
Not all projects need to be componentized if:
- Small project, simple interaction between modules, less coupling
- A module is not referenced by multiple external modules, just a single small module
- Modules do not need to be reused, and code is rarely modified
- Small team size
Then these projects don’t need to be componentized.
The most important part of componentization is the division of granularity, the more accurate the division, the better the effect of componentization. The partition of componentized granularity is not the smaller the better. Too small granularity will increase the cost of communication. Generally, componentization layers are divided into three layers:
- Business module
- General module
- Basic module
This is typically built from the base module, then the general module, and finally the business module.
Componentization needs to pay attention to the following points:
- Only the top can depend on the bottom
- Project common code resources sink
- Horizontal dependencies are best sunk
2. CocoaPods
How do we normally pull remote code locally? There’s one thing you need to find the remote code — CocoaPods. Using AFN as an example, you can find the AFN file in CocoaPods, then find the corresponding JSON version, and use the information inside to obtain the AFN.
Flow chart:
3. Componentized operations
3.1 Creating a Module
In Terminal, open the specified folder and enter pod lib create file name to generate a module. After the module is created, a project will be automatically opened.
Next, open the module and start writing code in classes.
After writing the code, perform a Pod Install operation so that you can see the module you created earlier.
3.2 Third-party libraries and other modules
If the file depends on some third-party library, you need to configure it.
Then re-pod Install.
The base library is the same, but you need to specify a path for it in your Podfile.
3.3 Obtaining module Resource Files
If you need a resource file for another module, you won’t find it. So how do you do that?
First put the pictures you want to use in Assets of the corresponding module.
Then add resource_bundles to your podSpec and re-pod install.
You can then retrieve the bundle fetch image.
The same is true for getting json files. Here you can see that if it’s a normal mainBundle fetch there’s a problem.
You’ll be fine if you get the JSON file based on the bundle.