Swift Framework?? Modularize your project with ‘Swift Framework’ in 30 seconds!

KD Knowledge Diet
4 min readApr 10, 2022

--

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

Create Project

I put it in /kingapp directory.

/kingapp directory will contain a newly created project

(2) Create Framework

Once you are done creating a project, create framework inside /kingapp directory.

Create Project

Now select Framework

Create a new framework

Create project in /kingapp directory. Note that directory structure is your choice. But I’m doing this to make you understand it easily.

Create a new framework in /kingapp directory

Check your directory to see if it’s well prepared.

/kingapp directory

Looks like there’s no problem to continue.

(3) Drag your framework(KingAppFramework) to your project(KingApp)

Drag KingAppFramework.xcodeproj to KingApp project.

Drag KingAppFramework.xcodeproj to KingApp project.

Popup will show. Click Save.

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.

Why is it red?

Don’t worry. You’ve followed so well. Just go back to /AppKing directory. Now you can see KingApp.wcworkspace is generated.

KingApp.wcworkspace

Now open KingApp.wcworkspace.

Well Integrated Local Framework

(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!

Add Framework
Added Framework

Code Time!

(1) Create a file in your integrated framework

Add any file you want

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!

Works fine!

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.

--

--

KD Knowledge Diet

Software Engineer, Mobile Developer living in Seoul. I hate people using difficult words. Why not using simple words? Keep It Simple Stupid!