Understanding Swift Code Using Diagrams

i.vikash
4 min readJul 30, 2022

--

Complex swift code not easy to understand ?

In this post I am trying to discuss about a very useful Xcode extension SwiftPlantUMLApp. Through which we can generate and view a class diagram for Swift code.

Let’s discuss about one of the scenarios, assume you are a developer and one already created project is assigned to you for modification. Now the 1st challenge for you will be to understand the code and the basic flow so that you can deep dive into the code and easily make the required modifications with confidence. So normally what you would do? Start reading the code and try to understand the objects, functions and it’s dependencies. If the code is easy you can quickly understand without any problem. But what if the code is complex and has lots of dependencies.

Example of AppDelegate :-

This diagram is created from AppDelegate class, having more than 500 lines of code.

If you look at this diagram you can easily get a basic idea about this class and it’s dependencies. After that when you read the same code you would we easily able to understand it in very less time.

Now let’s break it down into different sections and go through them one by one:-

  1. How to install in Xcode ?
  2. How to generate the diagram ?
  3. Utility of the diagram ?

How to install in Xcode :-

Installation process is very easy, checkout below link :-

Once installation is successfully done you can validate it in your Xcode

Open Xcode -> Select a swift file -> Editor -> SwiftPlantUML

How to generate the diagram :-

Generating a diagram is very easy by just following below steps :-

Open Xcode -> Select a swift file -> Editor -> SwiftPlantUML -> Open editable diagram in browser

It will open a diagram in browser in editable mode.

We can download this into different format as per our need like PNG, SGV and Txt.

Editable version gives us a flexibility to do modification, through which we can easily validate our implementation logic before making the changes into the code.

Utility of the diagrams :-

These diagrams are helpful for different purposes, I am trying to list out some of them which I am using :-

Understand codes and it’s dependencies :- Help to understand complex code visually without spending a lot of time.

Code refactoring : By looking into the diagrams, we can easily understand the dependencies and can do the code refactoring to improve the code quality.

Validate major changes before code modification : We can validate our major changes and dependencies by modifying this diagram before implementing into actual code.

For documentation : It will help us to create good documents from the existing source code, which other developers can use to understand it.

Make future improvements faster and more efficient : When we want to make some changes in our code after a long time we may forget lot of things, using this we can easily recap and make the required changes with confidence.

Any many more…

Conclusion:-

As we all know that understanding anything from a text file is not so as easy as compared to the diagrams. So in this post we have seen how we can create diagrams from our swift code. Also how we can utilize this diagram to save our time and effort.

Happy reading….

--

--

i.vikash

Tech enthusiast exploring software architecture, development, and innovation. Join me on my journey of tech discovery! 🚀 #TechExploration