Clean Swift Architecture: How a Developer Platform Standardizes the Creation of iOS Projects

A white man with glasses looking at a computer screen with programming codes.
See an example of how StackSpot EDP can be used with ViewCode to create standardized projects for iOS development using the Clean Swift Architecture.

Clean Swift Architecture is widely used in the iOS development ecosystem for its high modularity and scalability. However, its multiple layers can consume valuable developer time when creating the architecture’s files and base classes. In addition, code standardization can be compromised in large teams due to slight individual modifications.

Tools such as StackSpot EDP can be handy in solving this kind of problem. This article presents a simple example of how to use this tool to create standardized projects in iOS development. At the end of the process, we will create a scene automatically using Clean Swift Architecture and ViewCode, allowing developers to focus their productivity on non-repetitive tasks. In addition, we will highlight improvements and other possibilities that StackSpot can bring to this context.

StackSpot is an Enterprise Developer Platform (EDP) that accelerates the creation, distribution, and reuse of technology standards. You can standardize the use of technologies for each context. Hence, teams can create reliable software with an executable architecture, which can reduce the time between development of the first code and application in production.

The best part is that the CLI version of StackSpot EDP is available for personal use. Just download it and start using it.

This article is divided into creation process and template application.

Creating a template

1. First, install the StackSpot CLI

Follow the steps for downloading and installing the StackSpot EDP CLI on the Documentation page.

2. Next, access your terminal to create a Plugin named clean-swift-architecture-template using the command below:

stk create plugin clean-swift-architecture-template

Learn about the two types of Plugins and how to create your own.

3. After executing the command, the StackSpot CLI will ask for information about your Plugin. In this example, the ‘type’ attribute within the YAML indicates the type of Plugin you will create. In this case, the app type. 

Learn more about possible types of Plugins in the StackSpot EDP Documentation.

Terminal showing the inputs requested by the StackSpot CLI when creating a new plugin

4. After that, the StackSpot CLI creates the entire initial structure of a Plugin. You’ll see your Plugin’s folder with the structure of the YAML file. In addition, the CLI will indicate which command you should execute to apply the Plugin that will be created. 

Some artifacts are generated, but article focuses on the plugin.yaml file, which offers several interesting possibilities. We’ll address  two of them:

1. inputs

The user will request these input parameters when they apply the template to their project. In our example, these parameters will help materialize the .swift files generated after using the template.

2. hooks

Declarative hooks can be executed during any of the template’s application stages. The Plugin is used in two stages:

  1. Asking the user what data is required
  2. Using this data to fill in file templates and create the corresponding files

Learn more about the types of hooks in the StackSpot documentation.

Check out the plugin.yaml file below with the inputs and hooks required for the context of the Plugin you are creating and comments detailing each one:

Swift
schema-version: v2
kind: plugin
# general information about the plugin being created
metadata:
 name: clean-swift-architecture-plugin
 display-name: clean-swift-architecture-plugin
 description: › Clean Architecture Plugin
 version: 0.0.1
 picture: plugin.png
spec:
 type: app
 compatibility:
   - iOS
 docs:
   pt-br: docs/pt-br/docs.md
   en-us: docs/en-us/docs.md
 single-use: False
 repository: https://github.com/Yeltsinn/iOS-Studies-.git
 technologies: Swift
   - Api
 stk-projects-only: false
inputs:
# name of the project's main folder
 - label: Project name
   type: text
   name: project_name
   default: project-name
# name of the package, it will be the same name as the .xcodeproj file
 - label: Package Name
   type: text
   name: package_name
# name of the feature that will be created with the project
 - label: Feature Name
   type: text
   name: feature_name

hooks:
 # this action is responsible for locating the View file of the generated feature
 # and include the snippet with the ViewCode protocol methods

 - type: edit
   trigger: after-render
   path: "{{inputs.package_name}}/{{inputs.feature_name}}/{{inputs.feature_name}}View.swift"
   changes:
     - search:
         string: "extension {{inputs.feature_name}}View: ViewCodable {"
         insert-after:
           snippet: snippets/ViewCodeFunctions.swift
         when:
           not-exists-snippet: snippets/ViewCodeFunctions.swift

# action responsible for executing the xcodegen command at the end of the process
# and generating the .xcodeproj file
 - type: run
   trigger: after-render
   commands:
     - "xcodegen"

3. Storing the files that the Plugin will generate

Another critical component is the templates folder, containing everything materialized after the template is applied, such as general files and other folders.

In our example, we added the .swift files for each Clean Swift Architecture layer to this folder. The files will materialize after the template is applied.

When we use the feature_name input parameter declared in the abovementioned plugin.yaml file, it will be replaced by the value the user enters when the template is applied. 

The package_name parameter was used to generate the folder in which these files will be stored in the project.

Image showing the contents of the templates folder for the Clean Swift Architecture plugin

Finally, let’s create a folder called snippets. In this folder, we can insert files with snippets of code that we want to apply at different points in our template, making it leaner and avoiding code duplication.

Our example uses a snippet that contains the methods for a ViewCode protocol that will be applied to the UIView files. Check it out below:

Swift
func setupHierarchy() {

   }

   func setupConstraints() {

       let constraints: [NSLayoutConstraint] = [

           /* declare your constraints here */

       ]

       NSLayoutConstraint.activate(constraints)

   }

   func setupConfigurations() {

   }

Applying a Plugin

Once you’ve finished configuring the Plugin, it’s time to apply it when creating a project. To do this, execute the command indicated at the end of the Plugin creation process:

stk apply plugin /Users/myuser/clean-swift-architecture-plugin

After executing the command, the input parameters you defined in the plugin.yaml file should be requested, including project name, package name, and initial functionality.

Terminal requesting the parameters needed to apply the plugin.

In the end, the StackSpot CLI will generate the entire structure of an iOS development project with Login functionality files and using the Clean Swift Architecture as a base. It will then execute the xcodegen command according to one of the hooks defined in the plugin.yaml file, resulting in the creation of the .xcodeproj file.

Image showing the folder with the project structure generated using the newly created Clean Swift Architecture plugin. 

Consume innovation,
begin transformation

Subscribe to our newsletter to stay updated
on the latest best practices for leveraging
technology to drive business impact

Concluding thoughts

In this article, we’ve demonstrated how StackSpot EDP automates repetitive development tasks which often undermine productivity and open the door to a lack of project standardization within a team.

StackSpot offers multiple possibilities for evolution in the context of this article, for example, the creation of Plugins with the following functionalities:

  • Add Network layer for REST requests in the project
  • Application data encryption with KeyChain
  • Add data persistence layer with CoreData or another framework

The idea is that these plugins can add these capabilities to any iOS development project without requiring developers to re-implement them, thus increasing productivity and reducing the potential for bugs.

That’s it for today, folks. Do you have any questions or suggestions? Please leave a comment.

Consume innovation,
begin transformation

Subscribe to our newsletter to stay updated
on the latest best practices for leveraging
technology to drive business impact

Summary

Related posts

Download your free eBook and find new ways to evolve your company