Even if you do not want to share your code with other developers, you can still get huge benefits from creating a static library.
As Srikar Appal mentions the benefits of creating a static library: 1) Distribution of code , 2) Reuse of code , and I would also like to add, 3) Versions , 4) Tests (kudos to BergQuester below) and 5) Documentation .
Look at them more closely:
1) Code distribution
Static libraries are great because they make it easy to distribute your code. All you have to do is compile and share the resulting .a file.
Even if you never plan to share your code with other developers, you can still use it in your own projects.
Alternatively, you can include the static library project as a subproject in your main projects, which makes it dependent on the main project ... see https://github.com/jverkoey/iOS-Framework how this can be configured.
2) Code reuse
Even in a wide variety of applications, you will often find that you are performing the same task for which you previously wrote the code. If you're an efficient developer, you don't want to write the same code again ... instead, you would just want to add your previously written polished code.
You can say, But I can just include the classes directly .
What if your code is not necessarily polished , however? Or, as is usually the case, the framework that he uses changes over time?
When you make changes and bug fixes to a set of codes, it would be nice to be able to include the latest version in your projects (or to be able to easily update your projects in the future). The static library makes work easier because all related code is included in one package.
Also, do not worry about what other project-specific hacks were imposed on it by other developers - the main project cannot (or should not) change the static library code in the case of a static library included as a subproject.
This has an additional advantage: if someone needs to change the set of static libraries, he must make changes so that all projects that rely on him could still use it (without hackers for specific projects).
3) Versions
If you have a set of classes that move around and include the project in the project, it is difficult for him to keep up with the version. Most likely, the only thing you have is the main project.
What if one project fixes some errors and another project fixes other errors in this set of classes? Perhaps you do not know how to combine these changes (what if the two teams work separately, even for them)? Or each project can fix the same mistakes!
By creating a static library, you can track version control (the static library project has its own version number), and by making changes to the static library, you will have fewer merge problems and eliminate the risk of fixing the same errors again and again.
4) Test
As iOS continues to evolve as a platform, unit testing of your code is becoming more common. Apple even continues to expand and expand the test framework (XCTest) to simplify and speed up the work of iOS developers for unit tests.
While you (and, IMHO, should) write unit tests for your code at the application level, creating and encapsulating code with the IN static libraries usually makes these tests more convenient and maintainable.
Tests are better because well-designed static libraries encapsulate targeted functionality (that is, a well-designed library performs a specific task, such as network tasks), which simplifies the “unit” of code testing.
That is, a well-designed static library intends to fulfill a predetermined “goal”, therefore, in essence, it creates the boundaries of testing in a natural way (for example, networking and presenting the extracted data are likely to be at least two separate libraries).
Tests are easier to maintain, because they will be in the same repository (for example, Git repo) as the code of the static library (and thereby be updated and updated along with this code). Just as you do not want to copy and paste code from a project into a project, you also do not want to copy and paste tests.
5) Documentation
Like unit testing, embedded documentation continues to become more important in iOS.
While you can (and again IMHO, should) document the code at the application level, it is again better and easier to maintain if it is at the static library level (for the same reasons as the unit testing above).
To answer your question,
Do I really need to create a static library or just create a class for the inner code?
You can ask yourself the following:
- Will this code be used in multiple applications?
- Will there be more than one class in this code?
- Will several developers work or use this code at the same time (possibly in different applications)?
- Will this code be checked by the module?
- Will this document be documented?
If you answer YES to most of the above, you should probably create a static library for this code. This will probably save you trouble in the long run.
If you answer NO to most of the above, you may not get much benefit from creating a static library (since the code set should be very specific to the project in such an instance).