Swift Framework?? Modularize your project with ‘Swift Framework’ in 30 seconds!
--
During development, there are times when you feel burdened by the growing code base. Pause for a moment, and think about it. Have you ever had a hard time changing something in a big code base?
It’s time to learn swift modularization.
The Advantage of Framework over Swift Package and Library
This article will completely focus on how to integrate your custom Framework in your project. So, I will just briefly explain the difference between Swift Framework and Swift Package (and Swift Library).
Swift Framework is a hierarchical directory. It can include header files, resources, localized strings, storyboards, image files in a single package.
Project Time!
(1) Create Projects
I put it in /kingapp
directory.
(2) Create Framework
Once you are done creating a project, create framework inside /kingapp
directory.
Now select Framework
Create project in /kingapp
directory. Note that directory structure is your choice. But I’m doing this to make you understand it easily.
Check your directory to see if it’s well prepared.
Looks like there’s no problem to continue.
(3) Drag your framework(KingAppFramework) to your project(KingApp)
Drag KingAppFramework.xcodeproj to KingApp project.
Popup will show. Click Save.
Save Popup will show.
Now this is important.
You must set the path to KingApp
project and and make it the same name as KingApp. By doing this, wcworkspace will be automatically generated.
(4) Check Integration
Once you added your framework to your project, you will see this.
Don’t worry. You’ve followed so well. Just go back to /AppKing
directory. Now you can see KingApp.wcworkspace
is generated.
Now open KingApp.wcworkspace
.
(5) Final Step, add your framework to the project
It’s not done yet. but it’s almost done! You just have to add frameworks in your project!
Code Time!
(1) Create a file in your integrated framework
I added HelloFramework.swift file.
(2) Write Code!
Remember that all swift access modifier is internal
by default. You must make public functions, variables and classes for other packages or project to access your source code! Whenever you write code, build project again, and modifications you made will be applied.
public class HelloMyFramework{ public init() {} public var foo: String = "foo" public var bar: String = "bar"}
(3) Access your module in your project!
Looks like it’s working fine!
Conclusion
- Swift Framework is one of ways to create a module.
- Swift Framework is a hierarchical directory. It can include header files, resources, localized strings, storyboards, image files in a single package.
- Whenever you build a project, modifications made to your SwiftFramework are applied right away!
I personally use Swift Package much more when creating local dependencies but you also have to know that you can create swift framework in case you need to add local dependencies with resources.