In an earlier article we discussed how to create documentation for Swift projects and frameworks. In this article we will look at how to use DocC to generate documentation for shell scripts.
DocC is a documentation rendering tool built into Xcode that allows developers to easily build documentation for their code. Traditionally, developers would create documentation for their code via comments and then separately create documentation files for reference for other developers. DocC combines the 2 steps into a single step, making it easy for developers to write documentation and for others to read that documentation.
With a little modification we will be able to do the same for our shell scripts.
NOTE: We will be making use of concepts covered in an earlier article. Please make sure you go through that before going through the steps.
Adding DocC documentation for shell scripts
DocC is designed to help app developers easily create documentation for their code. So as such it is geared towards creating documentation for that. However, we can tweak things a little bit and use this process for our scripts.
We will be creating a macOS App project.This will act as a starting point. Don’t worry! We won’t be writing any code. You do not need to learn any programming language for this. So lets begin.
Open Xcode
Create a new macOS app project. Select File > New > Project from the menu bar.
Choose “Command line” from the template wizard.
Call it “FolderCreator” as thats the script we are using in this demo.
Provide your company’s url in reverse format as the Organisation identifier. For example, com.companyname, org.organisationname, and so on.
Choose the language as Swift. Save it on the desktop.
Add the script file to your project. We will be using the script from our scripting series. You can find the script here.
If we go ahead and build the documentation then we won’t find anything helpful. That’s because the documentation compiler is designed to read code and not our script. So it doesn’t contain anything useful. We will have to build the documentation for our script manually. Lets start by adding the documentation catalog. Select File > New > File, under the documentation heading select documentation catalog.
We will add metadata that specifies that this is the documentation for a script. The formatting style is similar to that of markdown documents. Much like the readme files you find in github.
We will add a description, a table listing out the different articles and a topics section that references articles.
Finally we will add a tab navigator to reference external links and resources.
Your completed file should look like:
# ``FolderCreator``
@Metadata {
@TitleHeading("Shell Script")
}
This script is used to create folders.
## Overview
This script is used to create upto 3 folders on the computer.
| Topic | Description | Link |
| --------- | --------------------------------------- | ---------- |
| License | License information | <doc:LicenseInformation> |
| History | Version history | <doc:History> |
| Usage | How to use the script | <doc:Usage> |
| Requirements | Prerequisites for running the script | <doc:Requirements> |
| Installation | How to install the script | <doc:Installation> |
| Help | Getting help | <doc:Help> |
| Script | Script file for reference | <doc:ScriptFile> |
| Man page | Man page file for reference | <doc:ManPage> |
| Download files | Download the different script versions | <doc:DownloadFiles> |
| Learn scripting | Blog article on learning how to write scripts | <doc:LearnScripting> |
## Topics
### Articles
- <doc:Help>
### Downloads
- <doc:DownloadFiles>
### Tutorials
- <doc:LearnScripting>
@TabNavigator {
@Tab("Github page") {
[https://github.com/AmaranthineTech](https://github.com/AmaranthineTech)
}
@Tab("Blog") {
[https://arunpatwardhan.com](https://arunpatwardhan.com)
}
@Tab("Youtube") {
[Amaranthine YouTube Channel](https://www.youtube.com/@amaranthine5616)
}
}
Let us add and article. We will add an article for usage. Click on File > New > File. Select Article from the template wizard.
In the overview add the information about using our script.
Your completed file should look like this:
# Usage
How to run the script
## Overview
- To create folders with default names run the command:
```shell
folderCreator
```
- To define your own folder names:
```shell
folderCreator <folder1> <folder2> <folder3>
```
- Available options : Only the help option is available
- Getting help : Use the -h or the -help options to get more information. Or you can use the man command to view the man page. Provided the man page was installed along with the command.
```shell
folderCreator -h
folderCreator -help
man folderCreator
```
> Important: It is assumed that the man page for the script has been deployed before use. For more information on how to deploy the man page visit [Shell scripting in macOS โ Part 10: Distribution](https://arunpatwardhan.com/2023/02/02/shell-scripting-in-macos-part-10-distribution/). You can also view the <doc:Installation> article.
Similarly add articles for Help, History, License information, Requirements, Learn scripting, Installation, and Download files. You can look at the comments from the script to build the information.
Add an article that will show the raw manpage file.
Copy and paste the entire manpage in that file as an “md syntax.
Your completed page should look like:
# ManPage
@Metadata {
@PageKind(sampleCode)
@CallToAction(url: "https://github.com/AmaranthineTech/ShellScripts/blob/main/folderCreator.1", purpose: link)
}
Man page file
## folderCreator man page
```md
.\"Copyright (c) 2015-2022 Amaranthine. All Rights Reserved.
.\"
.\"
.Dd August 10, 2021
.Dt FOLDERCREATOR 1
.Os macOS 11
.Sh NAME
.Nm folderCreator
.Nd Folder creation utility
.\"
.\" ============================================================================
.\" ========================== BEGIN SYNOPSIS SECTION ==========================
.Sh SYNOPSIS
.Nm
.Ar "folder names"
.Op verbs
.\" =========================== END SYNOPSIS SECTION ===========================
.\" ============================================================================
.\"
.\" ============================================================================
.\" ======================== BEGIN DESCRIPTION SECTION =========================
.Sh DESCRIPTION
.Nm
creates 3 folders in the home folder.
In case the folder names are not provided then the command
will create folders with default names "Tools", "Reports", "Help".
.Pp
The user is also prompted via the graphical user interface for names that should be used for the folders.
This is optional and the user can cancel it.
.Pp
There is also the option of getting help via the help verb.
.Pp
- This script is intended for creating the custom folders that are required on all corporate computers.
.Pp
- Run this script on a new computer or a computer being reassigned to another employee.
.Pp
- This script can run on all computers.
.\" ----------------------------------------------------------------------------
.\" ------------------------- BEGIN TERMINOLOGY LIST ---------------------------
.Sh VERBS
.Bl -hang
.It Op Fl h help
Both the options are used to invoke the help documentation.
.It Op Fl v version
Both the options are used to get the version number of the
.Nm
command.
.El
.\" --------------------------- END TERMINOLOGY LIST ---------------------------
.\" ----------------------------------------------------------------------------
.\" ============================================================================
.\" ======================== BEGIN REQUIREMENTS SECTION ========================
.Sh REQUIREMENTS
The following are the minimum requirements to get the script running.
.Bl -hang -offset 4n -width "xxxxxxxxxxxx" -compact
.It Shell:
zsh
.It OS:
macOS Big Sur 11.4 or later
.It Dependencies:
None
.El
.Ev HOME
.\" ============================================================================
.\" ======================== BEGIN INSTALLATION SECTION ========================
.Sh INSTALLATION
.Nm
can be installed anywhere you wish.
However, there are certain locations that are recommended.
.Bl -hang -offset 4n -width "xxxxxxxxxxxx" -compact
.It Location:
/Library/Scripts/
.It Permissions:
rwx r-x r-x
.El
.\" ============================================================================
.\" ======================== BEGIN USAGE SECTION ========================
.Sh USAGE
.Nm
.Ar folder1
.Ar folder2
.Ar folder3
.Pp
Will create folders with your own names.
.Pp
.Nm
.Ar -h
OR
.Nm
.Ar -help
.Pp
Will invoke the help utility.
.Pp
.Nm
.Ar -v
OR
.Nm
.Ar -version
.Pp
Will print the version number in stdout.
.Ss GUI Interaction
In all cases the user is always prompted for entering folder names via the graphical user interface.
Therefore this script triggers a gui popup.
In case this is not the desired behavior then the appropriate lines of code will need to be commented out.
.\" ============================================================================
.\" ======================== BEGIN WARNING/CAUTION SECTION ========================
.Sh WARNING/CAUTION
.Nm
does not perform any validation of names.
The only options that
.Nm
accepts are
.Ar -h
and
.Ar -help
verbs or the
.Ar -v
and
.Ar -version
verbs.
If the script does not see the
.Ar -h
,
.Ar -help
or the
.Ar -v
,
.Ar -version
options then it will assume that the data being passed in is the name of the folder.
The user of the
.Nm
command must ensure that the desired folder names are passed in.
The user will also be prompted, via the graphical user interface, if he/she wishes to provide the names for the folders.
If yes, then there will be subsequent
prompts asking for the folder names.
.\" ============================================================================
.\" ======================== BEGIN EXIT STATUS SECTION =========================
.Sh EXIT STATUS
In most situations,
.Nm
exits 0 on success
.\" ============================================================================
.\" ======================== BEGIN EXAMPLES SECTION ========================
.Sh EXAMPLES
.Nm
.Ar Resources
.Ar Results
.Ar Assistant
.Pp
This will create 3 folders
.Sy Resources
,
.Sy Results
,
.Sy Assistant
,
in the user's home folder.
.Pp
.Nm
.Pp
This will create 3 folders with the default names
.Pp
.Nm
.Ar Apps
.Pp
This will use the
.Sy Apps
name for the first folder but the default names for the last 2 folders.
.\" ============================================================================
.\" ======================== BEGIN DIAGNOSTICS SECTION ========================
.Sh DIAGNOSTICS
The script produces a log file called
.Sy ~/Library/Logs/folderCreator_log_v1-x.log
.Pp
This file is typically located in the user's home folder log folder.
The x represents the version number of
.Nm
.Pp
You can view the logs for each respective version.
.\" ============================================================================
.\" ======================== BEGIN COPYRIGHT SECTION ========================
.Sh COPYRIGHT
Copyright (c) Amaranthine 2015-2021.
All rights reserved.
https://amaranthine.in
.\" ============================================================================
.\" ======================== BEGIN CONTACT SECTION ========================
.Sh CONTACT DETAILS
.An Author: Arun Patwardhan
.Pp
Website: https://amaranthine.in
.Pp
Email: arun@amaranthine.co.in
```
Also create a page to show the actual script. This is useful. Anyone reading our documentation will also quickly be able to see our script.
And thats it. We have all the necessary information in there. You can download my completed project from the section below. Use it to compare the documentation that you wrote.
Distributing the documentation
There are several ways to distribute the documentation. I have covered those in the previous article. The best option is to host it on a website and make it available to anyone who wishes to use it. That is what I have done. You can view the website here: https://amaranthinescript.github.io/documentation/foldercreator
Use with other scripts
The good thing about the how we created our documentation is that those steps apply to all other kinds of scripts. Instead of our shell script you can add any other script such as AppleScript in there and create documentation for the same.
Yes the process is a bit manual but it does allow script writers to quickly create highly readable documentation.
DocC is a built-in documentation rendering tool that allows developers to easily build documentation for their code. Traditionally, developers would create documentation for their code via comments and then separately create documentation files for reference for other developers. DocC combines the 2 steps into a single step, making it easy for developers to write documentation and for others to read that documentation.
Requirements
This functionality is built into Xcode, so no other tool is required. You may need a hosting server incase you wish to host a web based version of the documentation. In this article we will be using Github pages to host our documentation.
Markdown comments
A key component to generate the documentation are comments that you have written. The comments must be formatted in a particular way so that Xcode can read them and use them to build the documentation. I have already covered this in an earlier article though we will have a look at some of those in a bit.
Creating additional documentation resources
The documentation tools generate documentation based on several resources:
Markdown comments
Type definitions, property declaration, and function declarations
available attribute
All these sources combined together provide a lot of information. But we are not just limited to these sources. We can add 2 other kinds of resources to our projects.:
Articles
Tutorial
Articles allow us to provide a little more context to the documentation. This is where advanced concepts such as functionality, underlying behavior, and things to know are presented to the user. It is possible to add diagrams and pictures to explain the concepts too.
Tutorials on the other hand allow the creator of the code to offer help to anyone who uses the API so that they can learn how to use the different features with the help of step by step instructions.
Both articles and tutorials add to the resources to make the documentation richer and more helpful.
Creating documentation for apps and packages
The process of creating documentation for apps/packages/frameworks is largely similar.
We will be using an example to understand how this goes. I will show only a small snippets of the code/comments here. You can download the completed project at the bottom of the file.
We will take a structured approach towards the design of our documentation.
First we will add the availability attributes
Second we will put comments for our code
Third we will add articles to our code
Fourth we will provide tutorials for our code
Adding availability attributes
Adding @available attributes is an important and useful part of the documentation process. It helps other users of your code know thing like which version of the language is required. What’s the minimum OS version that is required, and so on. All this becomes part of the documentation too. Let us look at how we can do this.
The code below represents a type called author. It’s a complete code but it’s missing the availability attributes. In fact, you should also see Xcode report an error for the link where we use Date.now saying that it’s only available from macOS 12 or later and that we should put an availability attribute for the same.
public struct Author {
public var name : String = ""
public var email : String = ""
public var dateOfBirth : Date = Date.now
public var phone : String = ""
public var photo : Data?
public var website : URL?
}
extension Author : CustomStringConvertible {
public var description: String {
let df : DateFormatter = DateFormatter()
df.dateStyle = .medium
df.timeStyle = .medium
return """
Author
----------
Name: \(self.name)
Email: \(self.email)
Birthday: \(df.string(from: self.dateOfBirth))
Phone: \(self.phone)
Website: \(self.website?.description ?? "")
"""
}
}
extension Author : Equatable {
public static func ==(lhs : Author, rhs : Author) -> Bool {
lhs.name == rhs.name && lhs.dateOfBirth == rhs.dateOfBirth
}
}
Let’s add the attributes and see how it looks. The code show now look like this:
@available(swift 5.0)
@available(iOS 14, macOS 12, *)
public struct Author {
public var name : String = ""
public var email : String = ""
public var dateOfBirth : Date = Date.now
public var phone : String = ""
public var photo : Data?
public var website : URL?
}
@available(swift 5.0)
@available(iOS 14, macOS 12, *)
extension Author : CustomStringConvertible {
public var description: String {
let df : DateFormatter = DateFormatter()
df.dateStyle = .medium
df.timeStyle = .medium
return """
Author
----------
Name: \(self.name)
Email: \(self.email)
Birthday: \(df.string(from: self.dateOfBirth))
Phone: \(self.phone)
Website: \(self.website?.description ?? "")
"""
}
}
@available(swift 5.0)
@available(iOS 14, macOS 12, *)
extension Author : Equatable {
public static func ==(lhs : Author, rhs : Author) -> Bool {
lhs.name == rhs.name && lhs.dateOfBirth == rhs.dateOfBirth
}
}
Adding comments
Let us continue with our previous example. Next we will add comments to the code. We will go use the markup features we saw in the earlier article. The comments should provide more details about the type. Things like version, copyright, date created, author, tips, contact details. In the case of functions you can have information about arguments and return types too. Let us add that to our code.
//
// File.swift
//
//
// Created by Arun Patwardhan on 04/07/23.
//
import Foundation
/**
Represents the author of the book.
**Protocols**
Conforms to `CustomStringConvertible` and `Equatable`
- version: 1.0
- note: The `name` and `dateOfBirth` are deemed to be unique properties.
- since: iOS 14, macOS 11
- author: Arun Patwardhan
- copyright: Amaranthine (c) 2023
- date: 3rd July 2023
- requires: Swift 5.x
- Tip: See the article on creating markup comments [Adding formatted Text to Swift](https://arunpatwardhan.com/2017/11/09/adding-formatted-text-to-swift-in-xcode/)
[arun@amaranthine.co.in](mailto:arun@amaranthine.co.in)
*/
@available(swift 5.0)
@available(iOS 14, macOS 12, *)
public struct Author {
public var name : String = ""
public var email : String = ""
public var dateOfBirth : Date = Date.now
public var phone : String = ""
public var photo : Data?
public var website : URL?
}
@available(swift 5.0)
@available(iOS 14, macOS 12, *)
extension Author : CustomStringConvertible {
public var description: String {
let df : DateFormatter = DateFormatter()
df.dateStyle = .medium
df.timeStyle = .medium
return """
Author
----------
Name: \(self.name)
Email: \(self.email)
Birthday: \(df.string(from: self.dateOfBirth))
Phone: \(self.phone)
Website: \(self.website?.description ?? "")
"""
}
}
@available(swift 5.0)
@available(iOS 14, macOS 12, *)
extension Author : Equatable {
public static func ==(lhs : Author, rhs : Author) -> Bool {
lhs.name == rhs.name && lhs.dateOfBirth == rhs.dateOfBirth
}
}
Note that we did not add comments to the extensions of the type. Also, it isn’t always necessary to add comments. Sometimes the types are fairly simple and self explanatory.
Creating the Documentation catalog
Now that we have added comments letโs add the DocC documentation.
In fact, you donโt have to do much. Simply select build documentation and it should create it for you with the code, comments, and availability information that is already there.
To create our documentation just select Product > Build Documentation from the menu.
You can see that already a lot of information is available without having to add any documentation.
The documentation we add will just build on this.
Creating Documentation Catalogs
The resources that we need for documentation are added within a documentation catalog. Items like Articles, Tutorial, Sample code, images are all added to the documentation catalog. These items are used to build our documentation. Let us add a documentation catalog to our project.
With our package open select File > New > File from the menu bar.
Choose the Documentation catalog option from the template wizard.
Click Next.
You should see the catalog added to your project.
Select the file called Documentation within the Documentation folder. This is the top level documentation file. We will place information about the package in here.
Rename this file to match the name of our project.
Next add the code shown below to our file. We will examine the different items in a moment.
# ``AmaranthineLibrary``
The types available in ths package are to be used in applications that work with books and collections of books.
## Overview

In this documentation we will look at the different types available. The idea behind these types is to support the creation of apps that work in different libraries. This should allow all kinds of institutions to quickly develop their own solutions for in-house libraries.
### Types
| Type | Description | | --------- | --------------------------------------- | | `Genre` | This describes the `Genre` of the book. | | `Book` | This represents a single book | | `Author` | This describes the author of the book | | `Library` | This describes the Library type. |
Under the resources folder add any image of your choice. I have an image called “library” and have added that in there.
That’s it for now. Let us look at what we have written.
First up a lot of the formatting you see is similar to markdown style. This means that many thing like headings with a ‘#’, designing tables are already familiar. Let us look at the first line.
# ``AmaranthineLibrary``
The double back tick is used to link to symbols in our project. In this case it is the framework itself. This can be a nested symbol too which is then indicated by a path. We will see an example of this when we create an article for the Author type.
Next we have text description giving us information about the framework. This is followed by the heading for Overview.

This line of code adds an image to the documentation. The text in the square brackets is the description followed by the name of the image file in round brackets. This name is the same as the name used while uploading the image in step 8. It is also possible to provide variations of the image. You can provide images with different scales and support for dark mode as long as you follow the correct naming convention.
<image name>~dark<scale>.<file extension>
For example, I could have provided different version of the library image.
library~dark@2x.png
library@2x.png
The system picks the correct one based on the need.
| Type | Description | | --------- | --------------------------------------- | | `Genre` | This describes the `Genre` of the book. | | `Book` | This represents a single book | | `Author` | This describes the author of the book | | `Library` | This describes the Library type. |
This creates a simple table. A single back tick is used to make the text appear in the code syntax.
- <enum:Genre>
This is another form of a link. This links directly to the Genre type documentation page. There are other ways of creating links to documentation pages. We see them in the code snippet below.
Now we haven’t created these articles yet but this is how we would create links to them. The name matches the name of the article itself. It could also be a link to a tutorial page.
@Small { MIT License }
Finally this allows us to include any fine print text we wish to add to our page. Build the documentation and look at the output. Notice that we get errors for the links to the articles as we have created them yet. For now we will delete the links to the LibraryInformation and the Tutorial table of contents.
Next we will look at creating articles.
Creating Articles
Articles allow us to provide more information about the different types that we have declared in our code. As I mentioned earlier. It allows us to add more information to the existing documentation that has been built. Let us go ahead and create the article for the Book type.
Click on File > New > File from the menu bar.
Select the document type as Article from the template wizard.
Name it BookInformation
Create it.
Add the following code to the article.
# BookInformation
The ``Book`` type represents a single book.
## Overview

The type is built up using several different properties. ``Book/author``, ``Book/genre``, ``Book/pageCount``, ``Book/publishedOn``, ``Book/title``, and ``Book/isbn``. Is a ``Book`` is to be uniquely identified then the ``Book/isbn`` property can be used for the same.
Add an image called ‘book’ to the resources folder.
Let us have a look at the different things added.
# BookInformation
The ``Book`` type represents a single book.
First up we have the title of the article. Then we have its description. Within the description there is a link for the Book type. Included using the double back ticks. This is a good way to help people reading the documentation to directly go over to the type itself.
## Overview

The type is built up using several different properties. ``Book/author``, ``Book/genre``, ``Book/pageCount``, ``Book/publishedOn``, ``Book/title``, and ``Book/isbn``. Is a ``Book`` is to be uniquely identified then the ``Book/isbn`` property can be used for the same.
Then we have the overview title with the image of a book. This is followed by a description along with links to properties within the type. Links to such properties are established using The path approach. Where we first mention the Type followed by a slash followed by the property. The rest of the article lists out the protocols that our type conforms to and the output format incase its to be printed.
Similarly we will add an article for the Author. Create a new article called author information. Add the code below.
The ``Author`` type represents the author of the book.
## Overview

This type is built up using 3 properties: ``Author/name``, ``Author/email``, and ``Author/dateOfBirth``. An instance of ``Author`` is said to be unique if both the ``Author/name`` as well as the ``Author/dateOfBirth`` are unique.
## Information > Note: The ``Author/name`` and ``Author/dateOfBirth`` are deemed to be unique properties.
> Important: Requires Swift 5.x
> Tip: See the article on creating markup comments [Adding formatted Text to Swift](https://arunpatwardhan.com/2017/11/09/adding-formatted-text-to-swift-in-xcode/)
Next we have the metadata. This tell Xcode how to handle the document creation. Should it merge the auto generated documentation with the contents of our article or should the contents of the article override the information. Here we are saying it overrides.
## Information > Note: The ``Author/name`` and ``Author/dateOfBirth`` are deemed to be unique properties.
> Important: Requires Swift 5.x
> Tip: See the article on creating markup comments [Adding formatted Text to Swift](https://arunpatwardhan.com/2017/11/09/adding-formatted-text-to-swift-in-xcode/)
This bit of code is also different. It creates sections for tips, notes, important information. It renders in the documentation with color highlights.
Build the documentation and see how it looks.
There are many other links and formatting options available.
You can control the page layout using tabs, tables.
You can add small disclosure text
Links can be added to specific properties and functions.
Extra top level documents: These are not articles related to a specific type but rather general information about the project.
Availability for the documentation
Comments: Items that are not rendered but are present for the creator of the documentation to take notes
Page appearance
Don’t forget to look at the completed code to see the different kinds of formats that have been used.
Next we will look at creating tutorials.
Creating tutorials
Tutorials as the name suggests are simple guides that walk through the usage of your code. Its a great way to help users of your code to learn how to use the types and functions that you have declared.
Tutorials are easy to create. Lets start of by creating the table of contents file.
Click on File > New > File from the menu bar.
Choose Tutorial Table of Contents.
Give it a name
Create it.
It should come pre-populated with some formatted text to show the table of contents.
Replace that with the code shown below. We will explore the different parts of the text in a moment.
@Tutorials(name: "Using the different types available") { @Intro(title: "How to use the different types") { In this tutorial we will look at creating and using the different types.
@Image(source: library.png, alt: "Library") }
@Volume(name: "Creating types") {
First we will look at how to create instances of the different types. @Image(source: create.png, alt: "Create")
@Chapter(name: "Author") { In this chapter we look at how to create objects of type ``AmaranthineLibrary/Author``. @Image(source: author.png, alt: "Author") @TutorialReference(tutorial: "doc:AuthorTutorial") }
@Chapter(name: "Genre") { In this chapter we look at how to create objects of type ``AmaranthineLibrary/Genre``. @Image(source: genre.png, alt: "Genre") @TutorialReference(tutorial: "doc:GenreTutorial") }
@Chapter(name: "Book") { In this chapter we look at how to create objects of type ``AmaranthineLibrary/Book``. @Image(source: book.png, alt: "Book") @TutorialReference(tutorial: "doc:BookTutorial") }
@Chapter(name: "Library") { In this chapter we look at how to create objects of type ``AmaranthineLibrary/Library``. @Image(source: library.png, alt: "Library") @TutorialReference(tutorial: "doc:LibraryTutorial") } }
@Volume(name: "Working with the library") { Next we will look at how all the types work together as a part of the library.
@Image(source: assemble.png, alt: "Assemble")
@Chapter(name: "Working with the library") { In this chapter we look at how to use the ``AmaranthineLibrary/Library`` object. @Image(source: library.png, alt: "Library") @TutorialReference(tutorial: "doc:UsingTheLibraryTutorial") } }
@Resources { Explore more resources for learning about the different features that we have used in Swift.
@Videos(destination: "https://www.youtube.com/channel/UC127UHd8V7bxPQYnd9QrN8w") { To view various blog articles and videos.
- [My Blog](https://www.arunpatwardhan.com/) }
@SampleCode(destination: "https://github.com/AmaranthineTech") { Download and explore sample code projects.
@Tutorials(name: "Using the different types available")
Right at the top we have the title for the tutorial. All the chapter and volume listings are within this block.
@Intro(title: "How to use the different types") { In this tutorial we will look at creating and using the different types.
@Image(source: library.png, alt: "Library") }
Then we have the introduction for the tutorial. Here we can give a brief introduction about the tutorial itself. We can add artwork to help illustrate things for the user.
@Volume(name: "Creating types") {
First we will look at how to create instances of the different types. @Image(source: create.png, alt: "Create")
@Chapter(name: "Author") { In this chapter we look at how to create objects of type ``AmaranthineLibrary/Author``. @Image(source: author.png, alt: "Author") @TutorialReference(tutorial: "doc:AuthorTutorial") }
... }
Next we provide the list of chapters. We can directly provide the list of chapters, or, if our tutorial covers different sections we can have multiple volumes each with a list of chapters. That is what I have done in this example.
The name of the volume, some text explains what is covered in this volume. With the chapter blocks in it. The chapters have a similar structure with name, text, image, and a link to the tutorial document.
@Resources { Explore more resources for learning about the different features that we have used in Swift.
@Videos(destination: "https://www.youtube.com/channel/UC127UHd8V7bxPQYnd9QrN8w") { To view various blog articles and videos.
- [My Blog](https://www.arunpatwardhan.com/) }
@SampleCode(destination: "https://github.com/AmaranthineTech") { Download and explore sample code projects.
At the end there is a resources block. This is a great place to put links to other resources that the reader may find useful. These can be categorized to give the reader more information. Here are some of the categories:
Documentation
Sample code
Videos
Forums
Downloads
Each of these can contain multiple links. Before we build the documentation let us add a tutorial document. In order to do that let us remove the extra volumes and chapters from the table of contents for the moment. This can be added later. Your final code should look like:
@Tutorials(name: "Using the different types available") { @Intro(title: "How to use the different types") { In this tutorial we will look at creating and using the different types.
@Image(source: library.png, alt: "Library") }
@Volume(name: "Creating types") {
First we will look at how to create instances of the different types. @Image(source: create.png, alt: "Create")
@Chapter(name: "Book") { In this chapter we look at how to create objects of type ``AmaranthineLibrary/Book``. @Image(source: book.png, alt: "Book") @TutorialReference(tutorial: "doc:BookTutorial") } }
@Resources { Explore more resources for learning about the different features that we have used in Swift.
@Videos(destination: "https://www.youtube.com/channel/UC127UHd8V7bxPQYnd9QrN8w") { To view various blog articles and videos.
- [My Blog](https://www.arunpatwardhan.com/) }
@SampleCode(destination: "https://github.com/AmaranthineTech") { Download and explore sample code projects.
Then we have the steps within the @Steps block. Each step is in its own @Step block. Note the difference between the two. The outer one is @Steps to indicate it holds a series of steps. Inside this is the @Step which represents a single step.
Each step contains the description for that step along with its @Code block. The way the tutorial works is that it walks the reader through a series of tasks that it performs. What is to be done is described in the text and a sample preview for the code is generated through the code file mentioned in the code block.
We will need to upload a series of code files. Each file contains additional code. Listing them in sequence generates the flow. Add the following files to the resources folder of your documentation. Name them BookCodeFile.swift, BookCodeFile-1.swift, BookCodeFile-2.swift, BookCodeFile-3.swift, and BookCodeFile-4.swift.
BookCodeFile.swift
//
// BookCodeFile.swift
//
//
// Created by Arun Patwardhan on 09/08/23.
//
import AmaranthineLibrary
let authorName : String = "Arun"
let authorEmail : String = "arun@mail.com"
let authorDOB : Date = Date(timeIntervalSince1970: 123456789)
let authorPhone : String = "9182736450"
let authorLink : URL = URL(string: "https://arunpatwardhan.com")
let arun : Author = Author(name: authorName,
email: authorEmail,
dateOfBirth: authorDOB,
phone: authorPhone,
photo: nil,
website: authorLink)
BookCodeFile-1.swift
//
// BookCodeFile.swift
//
//
// Created by Arun Patwardhan on 09/08/23.
//
import AmaranthineLibrary
let authorName : String = "Arun"
let authorEmail : String = "arun@mail.com"
let authorDOB : Date = Date(timeIntervalSince1970: 123456789)
let authorPhone : String = "9182736450"
let authorLink : URL = URL(string: "https://arunpatwardhan.com")
let arun : Author = Author(name: authorName,
email: authorEmail,
dateOfBirth: authorDOB,
phone: authorPhone,
photo: nil,
website: authorLink)
let bookGenre : Genre = Genre.educational
BookCodeFile-2.swift
//
// BookCodeFile.swift
//
//
// Created by Arun Patwardhan on 09/08/23.
//
import AmaranthineLibrary
let authorName : String = "Arun"
let authorEmail : String = "arun@mail.com"
let authorDOB : Date = Date(timeIntervalSince1970: 123456789)
let authorPhone : String = "9182736450"
let authorLink : URL = URL(string: "https://arunpatwardhan.com")
let arun : Author = Author(name: authorName,
email: authorEmail,
dateOfBirth: authorDOB,
phone: authorPhone,
photo: nil,
website: authorLink)
let bookGenre : Genre = Genre.educational
let bookStyle : BookStyle = BookStyle.paperback
BookCodeFile-3.swift
//
// BookCodeFile.swift
//
//
// Created by Arun Patwardhan on 09/08/23.
//
import AmaranthineLibrary
let authorName : String = "Arun"
let authorEmail : String = "arun@mail.com"
let authorDOB : Date = Date(timeIntervalSince1970: 123456789)
let authorPhone : String = "9182736450"
let authorLink : URL = URL(string: "https://arunpatwardhan.com")
let arun : Author = Author(name: authorName,
email: authorEmail,
dateOfBirth: authorDOB,
phone: authorPhone,
photo: nil,
website: authorLink)
let bookGenre : Genre = Genre.educational
let bookStyle : BookStyle = BookStyle.paperback
let bookTitle : String = "Introduction to Swift"
let bookISBN : String = "34243-3433-2"
let pageCount : Int = 987
let publicationDate : Date = Date(timeIntervalSince1970: 9876543210)
BookCodeFile-4.swift
//
// BookCodeFile.swift
//
// BookCodeFile.swift
//
//
// Created by Arun Patwardhan on 09/08/23.
//
import AmaranthineLibrary
let authorName : String = "Arun"
let authorEmail : String = "arun@mail.com"
let authorDOB : Date = Date(timeIntervalSince1970: 123456789)
let authorPhone : String = "9182736450"
let authorLink : URL = URL(string: "https://arunpatwardhan.com")
let arun : Author = Author(name: authorName,
email: authorEmail,
dateOfBirth: authorDOB,
phone: authorPhone,
photo: nil,
website: authorLink)
let bookGenre : Genre = Genre.educational
let bookStyle : BookStyle = BookStyle.paperback
let bookTitle : String = "Introduction to Swift"
let bookISBN : String = "34243-3433-2"
let pageCount : Int = 987
let publicationDate : Date = Date(timeIntervalSince1970: 9876543210)
let swiftTextBook : Book = Book(title: bookTitle,
author: arun,
publishedOn: publicationDate,
isbn: bookISBN,
pageCount: pageCount,
genre: bookGenre,
format: bookStyle)
Build the documentation and see how it renders the tutorial. It should look like this:
You can add preview images to your tutorial too to give a visual preview for your code. This is really useful when you are creating tutorials for UI based elements.
Adding assessments
One nice feature of tutorial is the ability to add assessments.
Assessments are a good way of helping readers determine if they have understood specific aspects of the code well. Itโs also a good way to drive home key concepts related to the code.
Assessments are added to the tutorial and is located at the bottom of the tutorial section. Add the following to our Book Tutorial:
@Assessments { @MultipleChoice {
Which of the following types is not used while creating an instance of a `Book`?
@Choice(isCorrect: false) { `float`
@Justification(reaction: "Try again!") { Have a look at the `Book` type to see what has been used. } }
@Choice(isCorrect: false) { `Data`
@Justification(reaction: "Try again!") { Have a look at the `Book` type to see what has been used. } }
@Choice(isCorrect: true) { `Author`
@Justification(reaction: "That's right!") { A `Book` has an `Author`. } }
@Choice(isCorrect: false) { `Bool`
@Justification(reaction: "Try again!") { Have a look at the `Book` type to see what has been used. } } } }
Let us explore each item in this.
@Assessments {
First we have our assessments block. All our multi choice questions go in here.
@MultipleChoice {
Which of the following types is not used while creating an instance of a `Book`?
Then we have the multi choice block along with the question itself.
@Choice(isCorrect: false) { `float`
@Justification(reaction: "Try again!") { Have a look at the `Book` type to see what has been used. } }
A MulitpleChoice block contains 2-4 choices. Each choice is represented with its own @Choice block. A choice block has a boolean flag indicating if its the right answer, the choice value, and a hint in the form of a justification to guide the reader to the correct value in case the choice isn’t correct.
Your complete tutorial should now look like:
@Tutorial(time: 10) { @Intro(title: "Creating an instance of Book.") { We will look at the steps involved in creating an instance of Book. }
@Section(title: "Create an Book object") { @ContentAndMedia { We will look at the steps involved in creating an instance of Book.
Which of the following types is not used while creating an instance of a `Book`?
@Choice(isCorrect: false) { `float`
@Justification(reaction: "Try again!") { Have a look at the `Book` type to see what has been used. } }
@Choice(isCorrect: false) { `Data`
@Justification(reaction: "Try again!") { Have a look at the `Book` type to see what has been used. } }
@Choice(isCorrect: true) { `Author`
@Justification(reaction: "That's right!") { A `Book` has an `Author`. } }
@Choice(isCorrect: false) { `Bool`
@Justification(reaction: "Try again!") { Have a look at the `Book` type to see what has been used. } } } } }
Build the documentation. Explore the tutorial and its assessment. It should look like this. The incorrect answers are highlighted in red while the correct one is in green.
That’s it. That covers the basic elements of creating documentation and tutorials for your code. Don’t forget to look at the completed code below.
Top level documentation and other markdown attributes
There are many kinds of attributes available for markdown. We have already seen some of them above. Lets look at a few more.
@MetaData
This attribute allows us to specify how DocC should build this document. Here are some of the items you can mention in there:
Attribute
Description
Possible values
@DocumentationExtension
Used to indicate if the contents of the article should override the default documentation or be appended to it.
override, append
@PageColor
Used to specify the color to be used for the banner at the top of the page
blue, gray, green, orange, purple, red, yellow
@TechnologyRoot
Used to indicate that this is a top level document and that it is not related to any specific type or code in the framework. This is useful when we want to provide some other information not related to the API in question.
@Available
Indicates the availability of the documentation itself.
Platform: iOS, macOS, watchOS, tvOS
@CallToAction
This is used to provide links to resources or downloads associated with that particular page.
Purpose argument can have: download, link
@PageKind
Used to specify if the page added is an article or a sample code that is being displayed.
article, samplecode
@PageImage
Used to provide an image for the page.
Purpose argument can have: icon, card
@DisplayName
Used to provide a custom name for a page rather than the symbol’s name.
String
@SupportedLanguage
Used to specify which programming language supports the specific feature.
swift, objc, objective-c
@Options
Similarly we can configure some options for the documentation. This controls how the documentation is rendered. It could be for a specific page or for all the pages in the API. Here are some of the options that we can configure.
Attribute
Description
possible values
@AutomaticSeeAlso
Used to indicate if the see also section is automatically created or not.
enabled, disabled
@AutomaticTitleHeading
Used to indicate if the title head is automatically created or not.
enabled, disabled
@TopicsVisualStyle
Used to specify how the topics on a page should be shown.
list, compactGrid, detailedGrid, hidden
@AutomaticArticleSubheading
Used to indicate if the article subheading is automatically created or not.
The above code generates a systematic structure like this:
@TabNavigator
We can offer information on a page with the help of a tab navigator too. This allows us to quickly show multiple options or related information in a structured way.
@TabNavigator { @Tab("add") {  }
@Tab("assemble") {  }
@Tab("author") {  }
@Tab("book") {  }
@Tab("checkout") {  }
@Tab("create") {  }
@Tab("find") {  }
@Tab("genre") {  }
@Tab("library") {  } }
This renders it as:
If there are fewer tabs then it renders slightly differently.
This renders into a simple list of links. You can choose to have it in a compactGrid style or detailedGrid style.
@Small
There is also a way to add small disclaimer or licensing text using the @Small block.
@Small { MIT License
Copyright (c) 2015 Amaranthine
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. }
This renders it as:
@Comment
Just like we can have comments in our code, we can have comments for our documentation too. The documentation builder does not render them and it is only meant for the author(s) of the documentation. This is a good way to put notes in for things that need to be done.
@Comment { Dont forget to change the name of this file. }
Exporting documentation
Now that we have seen different ways of documenting our code its time to start sharing it with our users. Of course when ever users of our package add the package to their project they can simply build the documentation as we have been doing so far. But in some situations users would like to go through the documentation before hand or would like to access it to check something. It is possible to export our documentation to make it accessible to them.
There are a couple of ways of exporting our documentation:
Directly export the documentation from the graphical user interface
Using the docc command from the command line interface
Let us look at both.
Exporting the documentation via the GUI
First build the documentation for your project.
Select the top level documentation file from the documentation window.
Hover over the right hand side of the documentation name. You should see a more button with 3 dots appear.
Click on the 3 dots and choose “Export”
Choose where you wish to save the archive.
Export it.
Now open the archive by double clicking on the file.
You should see the same documentation but under the imported catalog section.
Export using the command line
First make sure that your project is allready pushed and commited to the github archive.
Now we will be using the Swift-DocC plugin to generate the documentation. We need to add it as a dependency to the Swift Package. Update the Package.swift file to include the dependency.
// swift-tools-version: 5.8
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "AmaranthineLibrary",
platforms: [
.iOS(.v14),
.macOS(.v11),
],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "AmaranthineLibrary",
targets: ["AmaranthineLibrary"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "AmaranthineLibrary",
dependencies: []),
.testTarget(
name: "AmaranthineLibraryTests",
dependencies: ["AmaranthineLibrary"]),
]
)
Next we will run the swift command to generate documentation. Run the following command in your package folder.
swift package generate-documentation --source-service github --source-service-base-url https://github.com/AmaranthineTech/AmaranthineLibrary/blob/main --checkout-path /Users/instructor/Developer/AmaranthineLibrary/
Update the paths to match your own implementation. I have cloned the git repository in the /Users/instructor/Developer/ folder.
When you run the command it will tell you where the doccarchive is saved.
Copy the doccarchive and share it.
Open it to view the links to the different files. These links are generated thanks to the --source-service and --source-service-base-url options.
The links to the files should look like this:
This is one of the big advantages of generating the archive via the command line. You could also use the xcodebuild and xcrun to generate the documentation too.
Hosting the documentation
Exporting documentation is one way of sharing the documentation with users. But it would be even better if we could publish it as a webpage. Let us look at how to do that.
There are a couple of ways of publishing the documentation to a website.
File server
Web server with custom routing
Static pages on github
We will look at how to host them as static pages on github.
In order to host static pages on Github you will need a Github account. You can create one for free if you want.
There are 3 broad steps involved in hosting our documentation webpage on Github.
Creating the Github repository for hosting the webpages
Generating the publishable version of our documentation
Uploading the documentation to Github.
Let’s look at those 3 steps in detail.
Step 1: Creating a Github repository for hosting the web pages.
We are going to use a feature called Github Pages. As explained on the website:
GitHub Pages is a static site hosting service that takes HTML, CSS, and JavaScript files straight from a repository on GitHub, optionally runs the files through a build process, and publishes a website.
Github Documentation
There are 3 types of sites that can be hosted:
Type
Description
Sample URL
Project
The site is connected to a project on Github
User
The site is hosted in a repository owned by a personal user account.
<username>.github.io
Organisation
The site is hosted in a repository owned by an organisation account.
<organisation>.github.io
Depending on your needs you can go in for any one of those. For this demo we will be going in for an Organisation site.
The name of the repository will be in the format mentioned in the sample url above. So let us go ahead and create one.
Create an organisation on Github if needed. You can use an existing one if you want.
Next we will create a repository to host our website. Click on repository and create new.
We need to provide the name of our repository. It should follow the format: <organisation name>.github.io .
Provide a description, this is optional.
Set the site as public or private depending on your requirements.
Create the repository.
Navigate to the repository
Go to settings
Select the pages tab.
Make sure “Deploy from a branch” is sected under source.
Under branch select main and the folder as /root.
Your pages screen should look like this:
Go back to your code section of the repository.
Add a new file called readme.md. Put some basic text in it.
Switch back to the settings > pages section of the repository.
You should see a link to the repository.
Click on Visit site. You should see your readme.md file open in your browser. We will be replacing it with our docc documentation.
Let us clone this repository on our computer.
Now we can add our files there. Run the following commands. I will be creating the repository in the ~/Developer folder on my computer.
cd ~/Developer
git clone https://github.com/AmaranthineLibrary/amaranthinelibrary.github.io
cd amaranthinelibrary.github.io
mkdir docs
Step 2: Generating a publishable version of our documentation
Now that our repository is ready we will generate the documentation.
There are couple of ways of generating that documentation. However, we will simply extract it from the archive we created previously.
There are 2 because i added a top level documentation.
Final thoughts
There you go. We have successfully created, viewed, exported, and hosted documentation for our API using Swift DocC.
As you can see the process is fairly simple and straightforward. Yes, it does appear like its a lot of work, but building this practice will go a long way in making your code more useful, and easy to understand for anyone that’s using your code.
The best way to work with DocC is to start writing the comments, availability attributes, articles, and tutorials as you develop your code. This is far better than leaving it as a standalone activity.
So go ahead, use DocC in as many places as possible, even app projects. It will make life very simple.
Download
You can download the Swift Package Manager project here.
This is the last article in the series of scripts. We will take the script from Part 8 and continue with it. We will be taking our folderCreator script and using it in our distribution process.
Distribution is the process of placing the script along with any supporting resources on a destination computer making it easily available for use. There are many aspects to this process, but we are going to look at a few of them:
Where should our script be installed?
How will the end user be invoking the script?
Does the script have its own man page?
Creating our own commands from other shell scripts
You may have noticed that whenever we use commands like defaults, echo, ls, cd, … we do not have to specify the path to the command. There is a reason for that. When we run our script the shell interpreter ‘knows’ where to go and look for these commands. There are some predefined locations available. We can get these predefined locations from the environment.
printenv PATH
This gives us an output that looks like:
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
The output you see will vary from computer to computer. As we can see it is a list of folders separated by a ‘:’. These are the folders the interpreter will go through to locate the commands we use.
While we do not specify the path to a command in our script it is actually considered good practice. In fact, we will be making the same change to our script.
So if we want our own commands to be located easily it should be placed in one of these folders.
Installing commands
While installing commands there is one feature of macOS that we need to keep in mind: SIP (System Integrity Protection). Introduced in OS X 10.11 El Capitan, SIP prevents the modification of certain system files or folders even if you have root privileges. So this means that we do not have a lot of choice when it comes to installing our command. We will place it in /usr/local/bin folder.
Here is the final version of the folderCreator script. I have added paths to some commands and have kept the file name as folderCreator. The version number has been removed from the file name.
#!/bin/zsh
#-------------------------------------------------------------------------------------------------
#NAME: Folder creator
#AUTHOR: Arun Patwardhan
#CONTACT: arun@amaranthine.co.in
#DATE: 15th September 2022
#WEBSITE: https://github.com/AmaranthineTech/ShellScripts
#-------------------------------------------------------------------------------------------------
#LEGAL DISCLAIMER --------------------------------------------------------------------------------
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
#SOFTWARE.
#-------------------------------------------------------------------------------------------------
#LICENSE/TERMS AND CONDITIONS --------------------------------------------------------------------
#MIT License
#Copyright (c) Amaranthine 2021.
#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:
#The above copyright notice and this permission notice shall be included in all
#copies or substantial portions of the Software.
#-------------------------------------------------------------------------------------------------
#ABOUT -------------------------------------------------------------------------------------------
# fileCreator.zsh
# 1.8
#-------------------------------------------------------------------------------------------------
#DESCRIPTION -------------------------------------------------------------------------------------
# - THis script is intended for creating the custom folders that are required on all corporate computers.
# - Run this script on a new computer or a computer being reassigned to another employee.
# - This script can run on all computers.
#-------------------------------------------------------------------------------------------------
#USAGE -------------------------------------------------------------------------------------------
# - To create folders with default names run the command: ./folderCreator.zsh
# - To define your own folder names: ./folderCreator.zsh <folder1> <folder2> <folder3>
# - Available options : Only the help option is available
# - Getting help : Use the -h or the -help options to get more information. Or you can use the man command to view the man page.
#-------------------------------------------------------------------------------------------------
#WARNING/CAUTION ---------------------------------------------------------------------------------
#******************************************************************************************************************
#******************************************************************************************************************
#******************************************************************************************************************
#******************************************************************************************************************
# This script doesn't perform any validation of the folder names being passed in by the user.
# If the script does not see the -h or the -help options then it will assume that the data being passed in is the name of the folder.
# The user of the script must ensure that the desired folder names are passed in.
#******************************************************************************************************************
#******************************************************************************************************************
#******************************************************************************************************************
#******************************************************************************************************************
#-------------------------------------------------------------------------------------------------
#INSTALLATION ------------------------------------------------------------------------------------
# Instructions for placing the script in the correct place are listed here.
# Location: /Library/Scripts/
# Permissions: rwx r-x r-x
#-------------------------------------------------------------------------------------------------
#REQUIREMENTS ------------------------------------------------------------------------------------
# Shell: /bin/zsh
# OS: macOS Big Sur 11.4 or later
# Dependencies: None
#-------------------------------------------------------------------------------------------------
#HELP/SUPPORT ------------------------------------------------------------------------------------
# You can get help by running the following commands.
# ./folderCreator.zsh -h
# ./folderCreator.zsh -help
# OR
# man folderCreator.zsh
# You can also view the log file for the same at: ~/Library/Logs/folderCreator_log_v1-8.log
#-------------------------------------------------------------------------------------------------
#HISTORY -----------------------------------------------------------------------------------------
# ------------------------------------------------------------------------------------------------
# Version 1.0: Basic script which creates the folders
# Version 1.1: Gives user the ability to specify the folder names at run time.
# Version 1.2: Adds safety checks to the scripts
# Version 1.3: Includes documentation as well as ability to get help.
# Version 1.4: Includes optimisation using for loop
# Version 1.5: Prompts the user in the GUI for names for the different folders.
# Version 1.6: Updated the log mechanism with the help of a function and here document.
# Version 1.7: Replaced the folder variables with an array
# Version 1.8: Added absolute path for commands. Final version ready for deployment.
#-------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------
# ------------------------------ SCRIPT STARTS HERE ----------------------------------------------
#-------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------
#These are the default values used for the folder names incase the user doesn't provide any.
FOLDERS=("Tools" "Reports" "Help")
#Script version number
VERSION_NUMBER="1.8"
#Command name
COMMAND_NAME="folderCreator.zsh"
#1. Check to see if the user is asking for help. In which case we will have to provide information about the command.
if [[ $1 == "-h" ]] || [[ $1 == "-help" ]]; then
echo "ABOUT
-----
fileCreator.zsh
Version $VERSION_NUMBER
NAME
----
$COMMAND_NAME โ Folder creation utility SYNOPSIS
$COMMAND_NAME folder names [ verbs ]
DESCRIPTION
-----------
$COMMAND_NAME creates 3 folders in the home folder. In case the folder names are not provided then the command will create folders with default names 'Tools', 'Reports', \"Help\".
There is also the option of getting help via the help verb.
- This script is intended for creating the custom folders that are required on all corporate computers.
- Run this script on a new computer or a computer being reassigned to another employee.
- This script can run on all computers.
VERBS
-----
[ โh โhelp] Both the options are used to invoke the help documentation.
[ โv โversion] Both the options are used to get the version number of the folderCreator command.
REQUIREMENTS
------------
The following are the minimum requirements to get the script running.
Shell: zsh
OS: macOS Big Sur 11.4 or later
Dependencies: None
INSTALLATION
------------
$COMMAND_NAME can be installed anywhere you wish. However, there are certain locations that are recommended.
Location: /Library/Scripts/
Permissions: rwxr-xr-x
USAGE
-----
$COMMAND_NAME folder1 folder2 folder3
Will create folders with your own names.
$COMMAND_NAME -h OR $COMMAND_NAME -help
Will invoke the help utility.
$COMMAND_NAME -v OR $COMMAND_NAME -version
will print the version number in stdout.
WARNING/CAUTION
---------------
$COMMAND_NAME does not perform any validation of names. The only options that folderCreator accepts are -h and -help verbs or the -v and
-version verbs. If the script does not see the -h , -help or the -v , -version options then it will assume that the data being passed in is
the name of the folder. The user of the folderCreator command must ensure that the desired folder names are passed in. The user will also be
prompted, via the graphical user interface, if he/she wishes to provide the names for the folders. If yes, then there will be subsequent
prompts asking for the folder names.
EXAMPLES
--------
$COMMAND_NAME Resources Results Assistant
This will create 3 folders Resources , Results , Assistant , in the userโs home folder.
$COMMAND_NAME
This will create 3 folders with the default names
$COMMAND_NAME Apps
This will use the Apps name for the first folder but the default names for the last 2 folders.
NOTE
----
The user will be asked if he/she wishes to provide custom names in all the examples mentioned above. The user's value will always override
whatever is being provided to the script or defaults.
DIAGNOSTICS
-----------
The script produces a log file called ~/Library/Logs/folderCreator_log_v1-x.log
This file is typically located in the userโs home folder log folder. The x represents the version number of $COMMAND_NAME
You can view the logs for each respective version.
COPYRIGHT
---------
Copyright (c) Amaranthine 2015-2021. All rights reserved. https://amaranthine.in
EXIT STATUS
-----------
In most situations, $COMMAND_NAME exits 0 on success"
exit 0
fi
PATH_TO_LOG="$HOME/Library/Logs/folderCreator_log_v1-8.log"
# Function to log activity
function recordActivity() {
/bin/cat << EOF >> $PATH_TO_LOG
[$(date)] $1
EOF
}
echo "$(date) Running script $0 to create folders."
echo ""
TODAY=$(date)
recordActivity "Starting"
#2. Check to see if the version number is
if [[ $1 == "-version" ]] || [[ $1 == "-v" ]]; then
echo "Version: $VERSION_NUMBER"
exit 0
fi
#3. The following if statements check to see if the script is receiving any arguments. It then picks those arguments and assigns them to the respective variables for use in the script.
if [[ $1 != "" ]]; then
FOLDERS[0]=$1
fi
if [[ $2 != "" ]]; then
FOLDERS[1]=$2
fi
if [[ $3 != "" ]]; then
FOLDERS[2]=$3
fi
#4. We can prompt the user to see if they wish to provide folder names themselves. This will override any values provided as arguments.
userClicked=$(/usr/bin/osascript -e 'button returned of (display dialog "Would you like to provide names for the folders or use the defaults instead?" buttons {"Custom", "Default"} default button 2 with icon POSIX file "/System/Library/CoreServices/HelpViewer.app/Contents/Resources/AppIcon.icns")')
# if the user decides to provide custom names then go ahead and ask the user via GUI prompts. Otherwise use the values sent as arguments or defaults.
if [[ $userClicked == "Custom" ]]; then
recordActivity "The user decided to provide custom names."
FOLDERS[0]=$(/usr/bin/osascript -e 'text returned of (display dialog "Enter the name of folder 1" default answer "Utilities" buttons {"OK"} default button 1 with title "Folder that will hold the utilities" with icon POSIX file "/Users/Shared/Finder.icns")')
FOLDERS[1]=$(/usr/bin/osascript -e 'text returned of (display dialog "Enter the name of folder 2" default answer "Tools" buttons {"OK"} default button 1 with title "Folder that will hold the tools" with icon POSIX file "/Users/Shared/Finder.icns")')
FOLDERS[2]=$(/usr/bin/osascript -e 'text returned of (display dialog "Enter the name of folder 3" default answer "Help" buttons {"OK"} default button 1 with title "Folder that will hold the support documents" with icon POSIX file "/Users/Shared/Finder.icns")')
recordActivity "User provided: ${FOLDER[@]}"
else
recordActivity "User decided to use default values: ${FOLDER[@]}"
fi
#5. Go to the home folder.
cd $HOME
#6. Check to see if each of the folders exists. If it exists then do not create it. Else create the folder.
recordActivity "Creating folders: ${FOLDER[@]}"
for item in ${FOLDER[@]}; do
if [[ -d $item ]]; then
recordActivity "Not creating $item as it already exists."
else
recordActivity "Creating $item"
/bin/mkdir $item
fi
#7. Create the task completion file inside each folder.
recordActivity "Creating hidden file for $item folder."
cd $item
#8. Generate the file names based on the folder names.
/usr/bin/touch ".$item-FolderCreated"
cd ..
done
echo "$(date) Task completed. Have a nice day!"
#-------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------
# ------------------------------ END OF SCRIPT ---------------------------------------------------
In order to run this command we will have to give it execute capabilities.
chmod 755 folderCreator
This should change the icon of the script to that of an executable. If it doesn’t then go ahead and remove the extension from the file.
Copy the file to the /usr/local/bin folder. You will need to authenticate as admin to do this. There may be other executables in this folder.
Using commands
Now let us test to see if this has worked. Run the command:
folderCreator -h
We can use the which command to confirm that the correct binary is being used.
which folderCreator
Adding path to the environment
Now, it is not necessary that the executables we create should be placed in one of the standard PATH folders. We could place it anywhere else. All we would have to do is to export this new folder path to the PATH environment variable. There are a few ways of doing this.
Use the export command
Source another script file into your script
Automatically configure bash/zsh to source the export command
Using the export command
The export command temporarily adds a value to the PATH environment variable. We could do this at the start of the script. Let us look at this as an example.
Create a script, message.bash and save it in the /Users/Shared/Scripts/.
#!/bin/bash
echo "$(date) This is a random script $RANDOM"
Create another script called test.bash and save it where ever you want.
#!/bin/bash
export DEVELOPER_PATH=/Users/Shared/Scripts/
export PATH=$DEVELOPER_PATH:$PATH
bash welcome.bash
which welcome.bash
Run the script with the command: bash test.bash. Don’t forget to put the path when using the script.
The 2 export commands are setting the value of the PATH environment variable to the new path. Note that while doing that we still keep the existing PATH value. So the environment will contain all the existing PATHS as well as the new path.
We can immediately see the benefit of adding another folder to our PATH component. The commands on line 6 and 8 do not require the path to the welcome.bash script to be explicitly mentioned. Even though they are not in the standard search paths. In fact, any script/executable that is placed in that folder will now be directly accessible without having to specify the absolute path. Having said that it is still a good idea to put the absolute path to a command.
An important thing to keep in mind is that this change only applies to the script that we are running. This will not impact other shells or other scripts running in the same shell. The next 2 options will show us how we could possibly do that.
Source the export commands from another script
If the export commands are needed in more than one script then it might be a good idea to source them instead of rewriting them over and over again.
Start off by creating a new file called newPath.bash. It should only have the 2 export commands in them.
Now we will modify our test.bash script from the previous example as shown below.
#!/bin/bash
#Using source
source /Users/Shared/newPath.bash
bash welcome.bash
which welcome.bash
You can see that we simply source the original script in here. This is extremely useful if there are multiple paths that need to be added to multiple scripts. Any change in the path only has to be made in one place making this approach far more convenient and scalable. There is still the same catch. Any change to the path is only applicable to the scope of the script. Other scripts and the shell environment itself does not get affected.
Configure bash/zsh to source our export commands.
While the pervious 2 approaches are good, they have the limitation that the changes are only applicable to the script where the sourcing is done. All the other scripts and the shell itself are not affected by it. Now this maybe a desired outcome. There are situations where you would want this to be applicable globally to all the scripts and the shell itself without having to change the PATH value manually by ourselves. This can be done by changing the scripts that are invoked when the shell is loaded.
The file that is to be invoked is located in the /etc folder. It is called zshrc. If you want to make a change to bash shell then you need to modify bashrc. Copy this file to your desktop. Change it as shown below.
# System-wide profile for interactive zsh(1) shells.
# Setup user specific overrides for this in ~/.zshrc. See zshbuiltins(1)
# and zshoptions(1) for more details.
# Correctly display UTF-8 with combining characters.
if [[ "$(locale LC_CTYPE)" == "UTF-8" ]]; then
setopt COMBINING_CHARS
fi
# Disable the log builtin, so we don't conflict with /usr/bin/log
disable log
# Save command history
HISTFILE=${ZDOTDIR:-$HOME}/.zsh_history
HISTSIZE=2000
SAVEHIST=1000
# Beep on error
setopt BEEP
# Use keycodes (generated via zkbd) if present, otherwise fallback on
# values from terminfo
if [[ -r ${ZDOTDIR:-$HOME}/.zkbd/${TERM}-${VENDOR} ]] ; then
source ${ZDOTDIR:-$HOME}/.zkbd/${TERM}-${VENDOR}
else
typeset -g -A key
[[ -n "$terminfo[kf1]" ]] && key[F1]=$terminfo[kf1]
[[ -n "$terminfo[kf2]" ]] && key[F2]=$terminfo[kf2]
[[ -n "$terminfo[kf3]" ]] && key[F3]=$terminfo[kf3]
[[ -n "$terminfo[kf4]" ]] && key[F4]=$terminfo[kf4]
[[ -n "$terminfo[kf5]" ]] && key[F5]=$terminfo[kf5]
[[ -n "$terminfo[kf6]" ]] && key[F6]=$terminfo[kf6]
[[ -n "$terminfo[kf7]" ]] && key[F7]=$terminfo[kf7]
[[ -n "$terminfo[kf8]" ]] && key[F8]=$terminfo[kf8]
[[ -n "$terminfo[kf9]" ]] && key[F9]=$terminfo[kf9]
[[ -n "$terminfo[kf10]" ]] && key[F10]=$terminfo[kf10]
[[ -n "$terminfo[kf11]" ]] && key[F11]=$terminfo[kf11]
[[ -n "$terminfo[kf12]" ]] && key[F12]=$terminfo[kf12]
[[ -n "$terminfo[kf13]" ]] && key[F13]=$terminfo[kf13]
[[ -n "$terminfo[kf14]" ]] && key[F14]=$terminfo[kf14]
[[ -n "$terminfo[kf15]" ]] && key[F15]=$terminfo[kf15]
[[ -n "$terminfo[kf16]" ]] && key[F16]=$terminfo[kf16]
[[ -n "$terminfo[kf17]" ]] && key[F17]=$terminfo[kf17]
[[ -n "$terminfo[kf18]" ]] && key[F18]=$terminfo[kf18]
[[ -n "$terminfo[kf19]" ]] && key[F19]=$terminfo[kf19]
[[ -n "$terminfo[kf20]" ]] && key[F20]=$terminfo[kf20]
[[ -n "$terminfo[kbs]" ]] && key[Backspace]=$terminfo[kbs]
[[ -n "$terminfo[kich1]" ]] && key[Insert]=$terminfo[kich1]
[[ -n "$terminfo[kdch1]" ]] && key[Delete]=$terminfo[kdch1]
[[ -n "$terminfo[khome]" ]] && key[Home]=$terminfo[khome]
[[ -n "$terminfo[kend]" ]] && key[End]=$terminfo[kend]
[[ -n "$terminfo[kpp]" ]] && key[PageUp]=$terminfo[kpp]
[[ -n "$terminfo[knp]" ]] && key[PageDown]=$terminfo[knp]
[[ -n "$terminfo[kcuu1]" ]] && key[Up]=$terminfo[kcuu1]
[[ -n "$terminfo[kcub1]" ]] && key[Left]=$terminfo[kcub1]
[[ -n "$terminfo[kcud1]" ]] && key[Down]=$terminfo[kcud1]
[[ -n "$terminfo[kcuf1]" ]] && key[Right]=$terminfo[kcuf1]
fi
# Default key bindings
[[ -n ${key[Delete]} ]] && bindkey "${key[Delete]}" delete-char
[[ -n ${key[Home]} ]] && bindkey "${key[Home]}" beginning-of-line
[[ -n ${key[End]} ]] && bindkey "${key[End]}" end-of-line
[[ -n ${key[Up]} ]] && bindkey "${key[Up]}" up-line-or-search
[[ -n ${key[Down]} ]] && bindkey "${key[Down]}" down-line-or-search
# Default prompt
PS1="%n@%m %1~ %# "
# Useful support for interacting with Terminal.app or other terminal programs
[ -r "/etc/zshrc_$TERM_PROGRAM" ] && . "/etc/zshrc_$TERM_PROGRAM"
if [[ -f /Users/Shared/newPath.bash ]]; then
source /Users/Shared/newPath.bash
fi
Save this file back to the etc folder. It might be a good idea to take a backup of the original file in case we need to restore it back to undo any errors we might introduce.
Open the terminal app. Run the welcome.bash script without providing the path. See if it works.
Run the command to print the path variable:
printenv PATH
As we can see, there is no need to run the export command repeatedly. There is no need to source the file that contains those commands either.
Standard location or custom path
This approach does give us a lot of flexibility. However, we can see that there are several things we need to do before we can get everything working well. It might be better for us to place our commands in the standard /usr/local folder. That would make the deployment a lot easier.
Man pages
What are man pages?
If you have used the command line interface on macOS/Unix/Linux then you would be familiar with the man command. In case you aren’t then the man command is the command that loads the manual for the binary specified. It is a quick easy way to access the documentation and help for the command. However, man pages aren’t restricted to only binaries. They could be applied on normal files too.
There is one thing to keep in mind. It is not necessary that a man page exists for a given command. Try running the command man folderCreator. What do you get?
No manual entry for folderCreator
We get a message saying that there is no manual entry for our binary. So we need to go ahead and create one.
Before I talk about how to create them I will first address the question of whether we need to create one in the first place. Especially since we are already providing help view the -h -help options. Actually we don’t have to. However, keep in mind that most users are already familiar with the man command and their instinctive reaction is to look for the man page of your command. It would be very nice to offer them that ability.
How do we create them?
In order to create our man page we need to use certain macros that render the document for us. More information can be available via the mandoc, groff, mdoc, and man commands. I would highly recommend going through the man pages of these commands.
man pages are simple files that contain information which is formatted with the help of different macros. A typical man page contains the following sections in the specified sequence:
NAME
SYNOPSIS
DESCRIPTION
VERBS
REQUIREMENTS
INSTALLATION
USAGE
WARNINGS
EXIT STATUS
EXAMPLES
DIAGNOSTICS
COPYRIGHT
CONTACT DETAILS
There are other sections available too: the man page for groff command contains excellent information about that. Armed with the information about which sections are there within the man page we need to start gathering all our details together.
One piece that we need is the manual section.If we run the man command on man:
man man
It gives us some information about the manual sections. The sections describe the kinds of commands and potentially the actions they perform. Our command would fall under the user commands section or section 1.
Where are they located?
These files are located in the /usr/local/share/man/man1 folder. Where man1 represents the section number.
Let us try to create the file. Before that we will look at some macros that we would need.
Macro
Description
.Dd
This is used to specify the date when the man page was created/published.
.Dt
This is used to specify the title for the man page. Its value should always be in all caps.
.Os
The name of the operating system.
.Sh
Section header name,
.Nm
The name of the command. This is the name that will be used throughout the document.
.Nd
The description of the command.
.Ar
Arguments being passed to a command
.Op
Options being passed to a command
.Pp
New paragraph
.Bl
Start a list
.El
End the list
.Ev
For environment variables
.It
Italics
.Ss
Subsection
.An
Name of the author
.Sy
Symbolic font mode
.\"
Comment
Macros used to create the man page file
You can get more details and information about these macros by running the following command:
man mdoc
We will now use these macros to render our man page. The easiest way to do this would be to use an existing man page file as a template. The idea is to use the macros to do the rendering for us. Copy paste the contents of any existing man page into your file and start replacing the content with your own content. You can always test your page by using the man command directly on your file.
man /path/to/your/manpage/file.1
A good thing to do would be to add 1 item at a time and run the above command repeatedly till you get comfortable with how everything fits together.
If you need help correcting the formatting of the file run the following command:
mandoc -T lint folderCreator.1
This is how the man page file looks. Name it folderCreator.1 where the ‘1’ indicates the section number.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Our script has evolved quite a bit from the first blog. We started off with a very simple script and have ended with a larger script. It still does the same thing it did originally but is now a lot better. Here are some of the key points:
Writing event updates to log files
User interaction
Flexibility in terms of folder names via user interaction from the GUI as well as the command line arguments
Easy to maintain thanks to functions
Arrays make it scalable
Loops help make the script compact
Variables enhance scalability
Periodic checks ensure the script is safe and stable
User can get help using
the -h or the -help options
From the comments in the script
by viewing the man page for the script
Of course, there is room for improvement. Some of you might come up with ways of achieving this solution differently. Which is perfectly fine. There is no such thing as a universally perfect script. The point behind the script above was to illustrate how the different features could work together.
Final thoughts
Scripting is a continuous learning process. There are so many things in it. Over time you will find that you are faced with similar challenges. One thing that a lot of script writers and programmers do is to refer to perviously written scripts to get a head start. Every time you write a script it would be a good idea to archive it and keep a copy elsewhere. This will come in handy.
Custom operators are operators that are defined by us and are not part of the programming language natively.
We are all aware of the built in operators in the Swift Language.
Operators like: + – * % > == ! to name a few.
These operators are defined by the system. It is also possible for us to overload some of these operators. However there are situations where we would like to create our own operators that perform operations not defined by the system.
Thats exactly what Custom operators are. They are operators defined by the developer. These are not overloaded operators but completely new operators that donโt exist otherwise.
These operators are used within the project that we are working on. Though it is possible for us to share these operators using Swift Packages or XCFrameworks.
These operators are typically associated with a specific type and their behavior is also defined by us.
Why do we need them?
There are many reasons why we would want custom operators:
Allow for more compact and concise syntax.
Using custom operators allows our code to be more compact. Entire function calls can be condensed into a single operator.
Make the code more readable
This also improves the readability of our code. Properly chosen symbols can convey the message immediately and easily.
Allow for consistency in design of code
One of the other things that custom operators help us achieve is consistency. By using standard operations as operators we make our code more familiar and consistent to others who may read it. Programmers are familiar with the concept of operators and using them for different operations. So even if they may not immediately recognise the operator they would understand that there is some task for them to perform.
And finally it encourages reusability.
What do we need to create custom operators?
There are a couple of things that we need to create custom operators:
A logic for the action being performed by the operator
A list of valid symbols
Information about the operators attributes like prefix, postfix, infix.
The precedence of the operator if it is an infix operator
Operator Rules
There are some rules that must be followed when we are constructing the symbol for our operator. Most of the requirements are rather straightforward. However, choosing the right symbol is a very important task. There are a set of symbols that are allowed.
There are rules as far as whitespace around operators is concerned.
And finally there are certain symbols are allowed only in combination with other symbols.
Operator types
Type
Description
Prefix
Operators that appear before a variable or value. These are unary operators.
Postfix
Operators that appear after a variable or value. These are unary operators.
Infix
Operators that appear in between variables or values. These are binary operators.
Allowed Characters
This is the important bit. Which characters are allowed for usage as an operator.
We can have ASCII symbols that are used for builtin operators.
There are also many mathematical symbols that can be used as operators.
Note that the list of symbols show in the slide are not complete.
The next important bit is the whitespace around the operator.
If an operator has a whitespace on both the sides or doesnโt have whitespace on both the sides then it is interpreted as a binary operator. This is what would appear for infix operator.
If an operator has whitespace only on the left then it is a prefix unary operator.
If an operator has whitespace only on the right then it is a postfix unary operator.
If an operator does not have whitespace on the left but is followed by a dot then it is treated as a postfix unary operator.
Finally, any round, brace, square brackets appearing before or after the operator along with comma, colon, & semicolon are treated as whitespace
Making sure that we put the whitespace in the correct place while using these operators is very important.
No.
Rule
Example code
1
If an operator has a whitespace on both the sides or doesnโt have whitespace on both the sides then it is interpreted as a binary operator
a**b or a ** b
2
If an operator has whitespace only on the left then it is a prefix unary operator
**a
3
If an operator has whitespace only on the right then it is a postfix unary operator
a**
4
If an operator does not have whitespace on the left but is followed by a dot then it is treated as a postfix unary operator
a**.b is treated as a** .b
5
(, {, [ before the operator and ), }, ] after the operator along with ,, :, ; are treated as whitespace
There are some exceptions to the rules we just saw. Especially with exclamation mark & question mark.
! & ? which are predefined are always treated as postfix if there is no whitespace on the left
If we wish to use ? In optional chaining then it must not have whitespace on the left
To use it as a ternary conditional operator ?: it must have whitespace on both the sides
Operators with a leading or trailing <, > are split into multiple tokens. For example, in Dictionary<String, Array<Int>> the last 2 arrows are not interpreted as shift operator.
Operator grammar
There are rules for constructing operators. Only certain combinations are allowed.
Each operator contains a symbol which forms the operator head. The head is the first character in the operator.
The head may or may not be followed by 1 or more characters which are operator characters.
The head and the optional characters combined together form the operator.
The head itself can contain a one out of a set of valid symbols. Or it can contain a period.
These are some of the symbols allowed for usage as the head of the operator. You can choose any one of those.
/, =, -, +, !, *, %,<, >, &, |, ^, ?, ~
U+2055โU+205E
U+00A1โU+00A7
U+2190โU+23FF
U+00A9 or U+00AB
U+2500โU+2775
U+00AC or U+00AE
U+2794โU+2BFF
U+00B0โU+00B1
U+2E00โU+2E7F
U+00B6
U+3001โU+3003
U+00BB
U+3008โU+3020
U+00BF
U+3030
U+00D7
U+00F7
U+2016โU+2017
U+2020โU+2027
U+2030โU+203E
U+2041โU+2053
For the successive characters you can use any of the symbols allowed for the head plus some additional allowed symbols. The list above contains all the allowed symbols.
/, =, -, +, !, *, %,<, >, &, |, ^, ?, ~
U+2055โU+205E
U+00A1โU+00A7
U+2190โU+23FF
U+00A9 or U+00AB
U+2500โU+2775
U+00AC or U+00AE
U+2794โU+2BFF
U+00B0โU+00B1
U+2E00โU+2E7F
U+00B6
U+3001โU+3003
U+00BB
U+3008โU+3020
U+00BF
U+3030
U+00D7
U+0300โU+036F
U+00F7
U+1DC0โU+1DFF
U+2016โU+2017
U+20D0โU+20FF
U+2020โU+2027
U+FE00โU+FE0F
U+2030โU+203E
U+FE20โU+FE2F
U+2041โU+2053
U+E0100โU+E01EF
Examples
.+.
โ
โ
**
Operator Precedence
As far as infix operators are concerned there is also the question of precedence. Precedence is used to determine the operator priority when there are multiple operators in a single statement.
precedencegroup <#precedence group name#> {
higherThan: <#lower group names#>
lowerThan: <#higher group names#>
associativity: <#associativity#>
assignment: <#assignment#>
}
While the first 2 values are straightforward, they simply help determine the exact position of the newly created precedence as compared to existing precedences, the associativity and assignment are extra items that are not immediately clear.
Type
Description
Values
Associativity
Determines order in which a sequence of operators with the same precedence are evaluated in the absence of grouping brackets
left, right, none
Assignment
Specifies priority when used with optional chaining. TRUE: Same grouping rules as assignment operator from standard libraryFALSE: Same rules as operators that donโt perform assignment
true, false
The assignment of a precedence group specifies the precedence of an operator when used in an operation that includes optional chaining. When set to true, an operator in the corresponding precedence group uses the same grouping rules during optional chaining as the assignment operators from the standard library. Otherwise, when set to false or omitted, operators in the precedence group follows the same optional chaining rules as operators that donโt perform assignment.
Determines order in which a sequence of operators with the same precedence are evaluated in the absence of grouping brackets. so for example 4 – 6 – 7 has the minus sign which has left associativity. The operation 4-6 is grouped and then the – 7 operation is performed.
Nonassociative operators of the same precedence level canโt appear adjacent to each to other.
It is fairly easy to create our own operators. You can try the code in a playground. We will be creating 1 operator of each type: postfix, prefix, infix.
Create a new playground.
Declare the creation of the prefix operator as shown. This will be used as a squaring operator.
prefix operator **
Now we will provide a generic version of the operator implementation.
That’s it. It is that simple to create our own prefix operator. Now let us test it.
Create a variable of type Float and use the operator we have just created.
var lengthOfSideOfSquare : Float = 1.1
var areaOfSquare : Float = **lengthOfSideOfSquare
print("The area of a square whose side is \(lengthOfSideOfSquare) centimeters long is \(areaOfSquare) square centimeters")
Similarly declare a postfix operator. This one will perform conversion to a string.
postfix operator ~>
Now we will implement this operator. To do that let us make a simple type which will have the to string operator capability.
struct Person {
var name : String = ""
var age : Int = 0
}
extension Person {
static postfix func ~> (inputValue : Person) -> String {
return "NAME: \(inputValue.name)\nAGE: \(inputValue.age)"
}
}
Let us try this operator out and see.
var developer : Person = Person(name: "Arun Patwardhan",
age: 35)
var description : String = developer~>
print(#line, description)
Now let us implement an infix operator. The one that we are going to implement is a similarity operator which can be used to determine the degree of similarity between objects of the same type. To do that let us start off by declaring an enum which holds the values for the degree of similarity.
enum DegreeOfSimilarity {
case exactly_the_same
case almost_the_same
case slightly_similar
case completely_different
}
Infix operator can also have a precedence associated with it. Let us declare our own precedence and use it for our operator.
higherThan: This indicates that our precedence has higher priority than the Addition precedence
lowerThan: This indicates that our precedence has lower priority than the Multiplication precedence
Associativity: This indicates that our operator is not associative. So we cannot combine multiple occurrences of our operator in one statement.
assignment: This indicates that out operators has the same behaviour, as other operators that assign, when it comes to optional chaining.
Now we can declare our infix operator.
infix operator โ : DegreeOfSimilarityPrecedence
It is useful to save your new operator symbols as code snippets to easily use them. You can read this article if you don’t know how to create a code snippet.
Let us look at the implementation. I am going to use the same person type we used earlier.
var employee1 : Person = Person(name: "Jack",
age: 22)
var employee2 : Person = Person(name: "John",
age: 21)
var employee3 : Person = Person(name: "Jack",
age: 23)
var employee4 : Person = Person(name: "Jack",
age: 23)
print(#line, employee1 โ employee2)
print(#line, employee1 โ employee3)
print(#line, employee3 โ employee4)
Run the code and see the end result.
Feel free to create more operators and play around. You could also package these operators in a swift package and share them around. I have shared links to
Summary the new operator
Creating operators is very easy. Most of the requirements are rather straightforward. However, choosing the right symbol is a very important task.
The one thing that we should keep in mind is not to over use these. It can be tempting to do this. But abstracting everything can make the code look a little too vague.
So that is how you can create operators.
Download the sample project
I have uploaded some of the custom operators, that I have shown above, as a Swift Package. You can download the package as well as a demo project, which shows how to use them, from the links below.
Code snippets are as the name suggests, short pieces of code that can quickly be inserted into your code file. This is done either by dragging the snippet or by typing out the completion. Code snippets are very easy to create and use and can be applied in a wide variety of situations.
We will look at how you can create & use snippets. The following example is done in a playground, but this could be done from anywhere within Xcode.
Note: The example below was performed on Xcode 11.7
How do we create code snippets?
Start off by writing the code or text that you want to convert into a snippet. For example, I have a set of comments that I add at the start of every function. Write it down.
/**
This function performs a comparison of the 2 objects
- important: This function does not perform data validation.
- returns: `Bool`.
- requires: iOS 13 or later
- Since: iOS 13
- parameter lhsValue: This holds the value on the lhs of the operator
- parameter rhsValue: This holds the value on the rhs of the operator
- Example: `var answer = venueAddress == hotelAddress`
- author: Arun Patwardhan
- copyright: Copyright (c) Amaranthine 2020
- date: 14th September 2020
- version: 1.0
*/
2. Select it. 3. From the menu bar select Editor > Create Code Snippet.
This brings up the snippet editor. 4. Give your snippet the following details.
Option
Description
Name
This is the name of your code snippet.
Platform
This determines whether your snippet is available only for certain platforms: say only for iOS.
Availability
This determines the place where the snippet can be added.
Completion
This is the word that we will be typing in the Xcode editor to trigger the implementation of the snippet
Language
This specifies the language for which the snippet will be applied.
Name: Func Documentation
Language: Swift
Platform: All
Availability: All scopes
Completion: doc
Note that the values for Name and Completion can be whatever you want.
This is how the snippet should look.
5. Now we will try to use it in the editor. Start typing the completion word in the Xcode editor.
6. Select the snippet with your name and completion. 7. Hit enter. You should see the comments you want appearing in the editor.
Placeholder
We can make our snippet above even better by using placeholders. Placeholders are pieces of text that can be replaced by the user. They also give information about what is expected in the placeholder.
We can add place holders by simply typing the hint inside placeholder brackets. Placeholder brackets are nothing but open <# and closing #>. For example:
<# some text #>
Which appears as
The user will simply click on the “some text” placeholder.
There are plenty of places in our comments where we can use placeholders. When we use the code snippet it should put comments with place holders in them.
Let us change the comments in our Xcode editor first. We will edit the snippet later on. Make the changes as shown below.
/**
<# put the description of your function here #>
- important: <# mention some important points here #>
- returns: `<# return type #>`.
- requires: iOS <#iOS Version#> or later
- Since: iOS <#iOS Version#>
- parameter <#param 1#>: This holds the value on the lhs of the operator
- parameter <#param2#>: This holds the value on the rhs of the operator
- Example: `<#put some example code here#>`
- author: Arun Patwardhan
- copyright: Copyright (c) Amaranthine 2020
- date: <#day#> <#month#> <#year#>
- version: 1.0
*/
We have made the following items into comments.
Description
OS Version
Return type
Important comments
Parameter 1 & 2 names
Sample code
Day, Month, & Year
Of course, there are other things we could change too. Feel free to make any other changes you can think of.
2. Let us now copy these changes to the code snippet we created. Copy the code from the Xcode editor.
To bring the snippet editor again simply click on the add object button in the upper right hand corner of Xcode.
4. Select the snippet from the list on the left and click edit. 5. Paste the code that you just copied. Your snippet editor should look like this:
6. Click on ‘Done’ once you are finished making changes. Your snippet will now be ready.
7. Try adding the snippet into your editor just like before. Simply type in the completion for your snippet.
Dragging snippets
We can use the autocompletion we saw earlier. But it is also possible for us to drag snippets.
Exporting code snippets
Once created it is possible to export/import code snippets too. All the snippets are located in the following folder.
~/Library/Developer/Xcode/UserData/CodeSnippets/
Any snippets you have created will be located there.
Any new snippets to be added will have to be added there.
Summary
Code snippets are easy to create and have several advantages:
They improve the developers experience
Promote consistent code
Speeds up the process of writing code
Encourages developers to use each others snippets and gain the first 3 advantages.
Creating and using snippets is very very easy and has a lot of benefits. So go ahead and create snippets.
Xcode templates are basically pre-created files which we use when we create new projects or project files. So every time you go through the process of creating a new project File > New > Project > iOS > Single View App you are using the Single View App template.
While most of the templates are good enough we can easily create our own templates.
Why do we need custom templates?
The templates available out of the box are good for common situations. But we find that most of the times we end up creating a lot of file in our project. Sometime we implement common design patterns and architectures on a regular basis.
In such situations creating out own custom templates will help us save a lot of time during development.
The other advantage is that this promotes a more consistent development experience in any organisation.
Now that we know what templates are and why we may need custom templates let us look at how we can create them.
Template Types
Before we go ahead and create templates let us examine what a typical template includes.
Notice that there are 2 folders already created out here. File Templates & Project Templates. Let us browse through these folders.
File Templates
These are the templates used when a developer wishes to add a new file to an existing project. Under file templates you should see different folders in there. Each folder represents a certain category of templates. For example, User Interface is one category. Select it.
You should see multiple folders under it. The screenshot above shows the View template. As we can see the template itself is a folder with multiple files inside. The template ends with an extensions xctemplate. Let us look at those files.
___FILEBASENAME___.xib
TemplateIcon.png
TemplateIcon@2x.png
TemplateInfo.plist
The first one is the XIB file which will be generated by this template. The ___FILEBASENAME___ placeholder will be replaced with an actual name when it is created.
The next 2 are simply images that will be used as icons for the template when we bring up the template wizard in Xcode.
The last one is the more important one. The TemplateInfo.plist. This is where we describe how the file creation process works. This is also where we configure options which will be presented to the user. We will look at this file in greater depth later on when we try to create our own templates.
Project Templates
These are the templates that are used when a developer decides to create a new project. Under project templates you should see different folders in there. Each folder represents a certain category of templates. For example, Application is one category. Select it.
I have the single view app template inside it. This is the most commonly used template when starting out with iOS App Development. You should see other familiar project templates. Feel free to examine the files in the folder. Let us have a look inside the Single View App template folder. You should see these items:
ContentView.swift
Main.storyboard
TemplateIcon.png
TemplateIcon@2x.png
Preview Assets.xcassets folder
TemplateInfo.plist
The first 2 files are the UI related files. One of the 2 will be selected based on the users choice between Storyboard and SwiftUI.
The next 2 are simply images that will be used as icons for the template when we bring up the template wizard in Xcode.
The Preview Assets folder is used with SwiftUI for previewing purposes.
Here too we have the TemplateInfo.plist file which configures the template options at the time of creation. We will explore this file in greater depth when we try to create our own project template.
How can we create them?
In this article we will look at creating 2 types of templates.
File Templates
Project Templates
Warning: It may be a good idea to try this out on a test computer so that you do not break anything on the computer you use everyday.
Preparation
Before we get started let us prepare the folders where we will be storing our custom templates.
Navigate to the following folder.
~/Library/Developer/Xcode/Templates/
Note, you may have to create this folder.
There should be 2 folders inside: File Templates, Project Templates. If these folders are not there then go ahead and create them.
We will be placing our templates in these folders.
This article continues from the previous article. Earlier we saw how we can make iOS Apps without using the storyboard file. In this article we will explore how to implement Autolayout programmatically. We will continue from the previous article.
The code that I will be showing in the article will not be covering all the possible cases. The point of this article is to give you an idea on how to implement the different Autolayout solutions. Feel free to play around with the code to cover all the cases & situations.
Programmatic Constraints
We have 3 options when it comes to applying constraints programmatically:
StackViews
Layout Anchors
NSLayoutConstraints class
Visual Format Language (VFL)
Handling Size Classes in code
Handling Size classes in code is fairly easy. It is a simple question of overriding the correct function. We will look at this in greater detail when we cover the topic later in the article.
Apps which are designed without the help of Storyboard are called as “Nibless” apps. Normally we design an app with the help of a Storyboard file. Earlier they were called Xib files or Nib files. Hence the term “Nibless”.
Why should we create Apps without storyboard?
There are a number of reasons.
It makes for a better experience when implementing along with version control.
Allows us to create UI elements dynamically.
Makes reusable UI Components easier to distribute and reuse.
How can we create Apps without Storyboard?
There are a couple of things that need to be done. Firstly the Main.storyboard file needs to be removed and the project settings need to be updated to reflect this change.. We are doing this since we won’t be using the storyboard file. Everything will now have to be started up by us manually. Many of these tasks were taken care of by storyboard, but since that was removed we will have to do it. This means we have to manually create the window, create the view controller set it as a the root view controller. We also have to manually create each and every component on our own. That is the very thing we were trying to achieve.
This example is implemented on Xcode 10.3 on macOS 10.14.5. We are not implementing auto layout in this article. We will look at implementing that programmatically in the next article.
Let us start with an empty project. Open Xcode.
Select File > New > Project
Give it any name. Select the language as Swift & leave the checkboxes unchecked.
Once the project loads select the Main.storyboard file and delete it.
Switch to the Project settings file.
Remove the entry for the main interface.
It is a good idea to leave the LaunchScreen.storyboard file. The reason for this is to give the launch process a reference of the screen size it needs to produce. Else it will default down to the 0,0,320,480 which is the old iPhone size.
Switch to the AppDelegate.swift file.
Add the following property below the UI Window declaration.
let mainScreenController : ViewController = ViewController()
Add the code to create the window and set root view controller in the didFinishLaunchingWithOptions method
//1. Create the UIWindow object
self.window = UIWindow(frame: UIScreen.main.bounds)
//2. Set the root view controller
self.window?.rootViewController = self.mainScreenController
//3. Make the window key and visible
self.window?.makeKeyAndVisible()
Switch to the ViewController.swift file.
Declare the following variables
//UI Variables
var labelDemo : UILabel?
var imageDemo : UIImageView?
var buttonDemo : UIButton = UIButton(type: UIButton.ButtonType.roundedRect)
var dataField : UITextField?
Implement the function to create labels. The process of creating a view programmatically is fairly straightforward. Barring a few variations depending on the view component nothing is drastically different.
func createLabel()
{
//1. Specify the dimensions
let labelRect : CGRect = CGRect(x: 100.0, y: 50.0, width: self.view.frame.size.width - 130.0, height: 60.0)
//2. Create the view object
labelDemo = UILabel(frame: labelRect)
//3. Customise the view attributes
labelDemo?.text = "This is my first Programmatic App."
labelDemo?.textColor = UIColor.yellow
labelDemo?.textAlignment = NSTextAlignment.left
labelDemo?.numberOfLines = 0
labelDemo?.font = UIFont.boldSystemFont(ofSize: 20.0)
//4. Add the view to the subview
self.view.addSubview(labelDemo!)
}
Let us examine the steps one by one.
//1. Specify the dimensions
let labelRect : CGRect = CGRect(x: 100.0, y: 50.0, width: self.view.frame.size.width - 130.0, height: 60.0)
This will define the dimensions of the view. As we are not implementing auto layout we will need to do this manually.
//2. Create the view object
labelDemo = UILabel(frame: labelRect)
Now that we have the dimensions we can go ahead and instantiate an instance of the label object using those dimensions. These 2 parts are the same as dragging a label from the object library onto the storyboard and placing it onto the storyboard per our requirements.
//3. Customise the view attributes
labelDemo?.text = "This is my first Programmatic App."
labelDemo?.textColor = UIColor.yellow
labelDemo?.textAlignment = NSTextAlignment.center
labelDemo?.numberOfLines = 0
labelDemo?.font = UIFont.boldSystemFont(ofSize: 20.0)
This part is the same as changing the attributes in the attributes inspector. This is where we customise the label.
//4. Add the view to the subview
self.view.addSubview(labelDemo!)
This last part also forms one part of dragging the label on to the storyboard. When we drag a view on to the storyboard it is placed within the main view that belongs to the ViewController. This statement completes the above process.
Repeat the above steps for showing an image.
func createImage()
{
//1. Specify the dimensions
let imageRect : CGRect = CGRect(x: 30.0, y: 50.0, width: 60.0, height: 60.0)
//2. Create the image model
let imageModel : UIImage = UIImage(named: "logo.png")!
//3. Create the view object
imageDemo = UIImageView(frame: imageRect)
//4. Customise the view attributes
imageDemo?.image = imageModel
imageDemo?.contentMode = UIView.ContentMode.scaleAspectFit
//5. Add the view to the subview
self.view.addSubview(imageDemo!)
}
The code above is almost similar to the one created for labels except for the fact that we had to explicitly create a model object for the view. Images being different from strings, require this process to be done explicitly.
Similarly let us implement the code for creating buttons
func createButton()
{
//1. Specify the dimensions
let buttonRect : CGRect = CGRect(x: 30.0, y: 220.0, width: 100.0, height: 50.0)
//2. Provide the frame to the button
buttonDemo.frame = buttonRect
//3. Customise the view attributes
buttonDemo.setTitle("Click Me", for: UIControl.State.normal)
buttonDemo.addTarget(self, action: #selector(ViewController.clickMeTapped), for: UIControl.Event.touchDown)
//4. Add the view to the subview
self.view.addSubview(buttonDemo)
}
@objc func clickMeTapped(
{
print("Click me tapped!")
}
Again just minor variations here. Mainly the step to add a target function to be invoked when the button is tapped. We also need to write the target function itself.
We will also implement the code to create a text field.
func createTextField()
{
//1. Provide dimensions for the view
let tfRect : CGRect = CGRect(x: 30.0, y: 140.0, width: self.view.frame.size.width - 60.0, height: 50.0)
//2. Create the view object
dataField = UITextField(frame: tfRect)
//3. Customise the attributes of the view
dataField?.placeholder = "Enter Name"
dataField?.borderStyle = UITextField.BorderStyle.roundedRect
dataField?.keyboardType = UIKeyboardType.namePhonePad
dataField?.keyboardAppearance = UIKeyboardAppearance.dark
dataField?.returnKeyType = UIReturnKeyType.go
//4. Add the view to the subview
self.view.addSubview(dataField!)
}
Next we need to call all these functions. I have implemented a single creator function for that.
I have also added code to change the background colour so that we can see the background clearly.
Run the project. Everything should appear normally.
Are there any benefits of creating apps without storyboard?
The points mentioned in the “why should we make programmatic apps?” section are some of the advantages. Beyond that there aren’t too many. If you are looking at a team based project development then this approach is good. There is no difference in terms of memory or performance when it comes down to apps design with or without storyboard.
Are there any drawbacks?
As can be seen from the example above, there are a couple of drawbacks
The main drawback is that you can’t get a quick preview of how your app looks. You have to run the simulation every time you wish to see the end result.
There is a lot more coding involved. Which can be daunting to those who are overly accustomed to designing with the help of storyboards
Note
A small point. I have left the LaunchScreen.storyboard file. I did not delete it. The reason I did that was to allow the app to allow the system to determine the dimensions on the device. If we do delete the file then the UIScreen.main.bounds return (0.0, 0.0, 320.0, 480.0) which are the old iPhone screen size settings. While you can go ahead and make changes programmatically it is a lot easier to just leave the LaunchScreen.storyboard file there.
Carrying on from the previous point. It actually is okay if you leave the Main.storyboard file as is too. In which case you will have to skip steps 5,6,8,9,10. The code is still running programmatically but you do not have to create the main ViewController manually.
Download the Source Code
You can download the Xcode Project from this link.
In an earlier article I had discussed how we can create our own frameworks to easily share reusable code. In this article we will take this a little further and create our own reusable UI Components.
Points to Note:
The reusable component we will be creating is based on UIKit. For that reason this component can only be used in iOS Apps. However, you can follow the same steps to create a reusable component for macOS using Cocoa.
UI Components, distributed through a framework, do not render in the project storyboard file.
You should be familiar with creating Embedded Binaries (your own Framework). If you aren’t then please read this article first.
These projects are created on Xcode 10 with Swift 4.2
Getting Started
We will complete the implementation as a 2 step process. Here is the screen that we are planning to implement.
Creating the Reusable Framework
Open Xcode and create a new Framework project.
Name the project “UIVIdentityCard”.
Save the project in any folder.
Create a new Swift file File > New > File > Swift.
Name the file “GenderType.swift”. This is where we will declare the enum that holds the Gender type that we are creating.
Add the following code to the file.
import Foundation
/** Possible scores that can be given.
*values*
`Male`
`Female`
`NotSpecified`
*functions*
`func toString() -> String`
Used to get the `String` version of the value
- Author: Arun Patwardhan
- Version: 1.0
*/
public enum GenderType
{
case Male
case Female
case NotSpecified
}
/** This extension adds the Enum to String converions capability
- Author: Arun Patwardhan
- Version: 1.1
*/
extension GenderType
{
/** This function converts from enum value to `String`
- important: This function does not do validation
- returns: `String`.
- requires: iOS 11 or later
- Since: iOS 11
- author: Arun Patwardhan
- copyright: Copyright (c) Amaranthine 2015
- version: 1.0 */
@available(iOS, introduced: 11.0, message: "convert to String")
func toString() -> String
{
switch self
{
case .Male:
return "Male"
case .Female:
return "Female"
case .NotSpecified:
return "Not Specified"
}
}
}
Create a new Swift file called “PersonDetailsModel.swift”.
Add the following code to the file.
import Foundation
/** This struct represents the data that is to be shown in the ID card
**Variables**
`personName`
`personIcon`
`personDob`
Date of Birth
`personAddress`
`personPhone`
`personEmail`
`personCompany`
`personHeight`
`personWeight`
`personGender`
**Important** There is a variable with the name `entryCount`. This variable keeps tracks of the number of stored properties that exist. The value of this variable will be used to determine the number of rows in the table.The computed property `numberOfRows` is the property used to access the value of `entryCount`.
- Author: Arun Patwardhan
- Version: 1.0
*/
public struct PersonDetailsModel
{
internal var entryCount : Int = 7
public var personName : String = ""
public var personIcon : UIImage
public var personDob : Date
public var personAddress: String = ""
public var personPhone : String = ""
public var personEmail : String = ""
public var personCompany: String = ""
public var personHeight : Double? = 0.0
{
willSet
{
if newValue == nil & personHeight != nil
{
entryCount -= 1
}
else if newValue != nil & personHeight == nil
{
entryCount += 1
}
}
}
public var personWeight : Double? = 0.0
{
willSet(newValue)
{
if newValue == nil & personWeight != nil
{
entryCount -= 1
}
else if newValue != nil & personWeight == nil
{
entryCount += 1
}
}
}
public var personGender : GenderType?
{
willSet
{
if newValue == nil & personGender != nil
{
entryCount -= 1
}
else if newValue != nil & personGender == nil
{
entryCount += 1
}
}
}
public var numberOfRows : Int
{
return entryCount
}
public init(withName newName : String, icon newIcon : UIImage, birthday newDob : Date, address newAddress : String, phone newPhone : String, email newEmail : String, Company newCompany : String, height newHeight : Double?, weight newWeight : Double?, andGender newGender : GenderType?)
{
personName = newName
personIcon = newIcon
personDob = newDob
personAddress = newAddress
personPhone = newPhone
personEmail = newEmail
personCompany = newCompany
if newGender != nil
{
entryCount += 1
}
if newWeight != nil
{
entryCount += 1
}
if newHeight != nil
{
entryCount += 1
}
personHeight = newHeight
personWeight = newWeight
personGender = newGender
}
}
/** This extension adds protocol conformance for the `CustomStringConvertible` protocol.
- Author: Arun Patwardhan
- Version: 1.1
*/
extension PersonDetailsModel : CustomStringConvertible
{
public var description: String
{
return """
NAME: \(self.personName)
DATE OF BIRTH:\(self.personDob)
ADDRESS: \(self.personAddress)
EMAIL:\(self.personEmail)
PHONE:\(self.personPhone)
"""
}
}
Now we will focus out attention on the View. Create a new file File > New > File > View.
Name the view “UIVIdentityCard.swift”.
Design the view as shown in the screenshot below.
Create the corresponding“UIVIdentityCard.swift” file.
Make the IBOutlet & IBAction connections for the different UI elements.
Add the following code. This is how your file should look after its completed.
/** The UIVIdentityCard class
**Functions**
`public func load(data newPerson : PersonDetailsModel)`
Used to load the data for the view.
- Author: Arun Patwardhan
- Version: 1.0
*/
@IBDesignableopen class UIVIdentityCard: UIView, UITableViewDelegate, UITableViewDataSource
{
//IBOutlets --------------------------------------------------
@IBOutlet public weak var personIcon : UIImageView!
@IBOutlet public weak var personName : UILabel!
@IBOutlet public weak var personDetails : UITableView!
//Variables --------------------------------------------------
public var localTableData : PersonDetailsModel!
let nibName : String = "UIVIdentityCard"
var view: UIView!
let cellIdentifier : String = "IDCard"
//Functions --------------------------------------------------
/** This function does the initial setup of the view. There are multiple things happening in this file.
1) The first thing that we do is to load the Nib file using the `nibName` we saved above. The UNIb object contains all the elements we have within the Nib file. The UINib object loads the object graph in memory but does not unarchive them. To unarchive them and get the ibjects loaded completely for use we have to instatiate the object and get the arry of top level objects. We are however interested in the first object that is there in the array which is of type `UIView`. The reference to this view is assigned to our local `view` variable.
2) Next we specify the bounds of our view
3) Finally we add this view as a subview
- important: This function does not do validation
- requires: iOS 11 or later, the varibale that contains the name of the nib file.
- Since: iOS 11
- author: Arun Patwardhan
- copyright: Copyright (c) Amaranthine 2015
- version: 1.0
*/
@available(iOS, introduced: 11.0, message: "setup view")
func setup()
{
//1)
self.view = UINib(nibName: self.nibName, bundle: Bundle(for: type(of: self))).instantiate(withOwner: self, options: nil)[0] as! UIView
//2)
self.view.frame = bounds
//3)
self.addSubview(self.view)
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
if let count = localTableData?.entryCount
{
return count - 2
}
return 0
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell
{
var cell : UITableViewCell? = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)
if nil == cell
{
cell = UITableViewCell(style: .default, reuseIdentifier: cellIdentifier)
}
switch indexPath.row
{
case 0:
let formatter = DateFormatter()
formatter.dateStyle = .medium
cell?.textLabel?.text = "Birthday\t: "+ formatter.string(from: (localTableData?.personDob)!)
case 1:
cell?.textLabel?.text = "Email\t: " + localTableData.personEmail
case 2:
cell?.textLabel?.text = "Phone\t: " + localTableData.personPhone
case 3:
cell?.textLabel?.text = "Address\t: " + localTableData.personAddress
case 4: cell?.textLabel?.text = "Company\t: " + localTableData.personCompany
case 5:
cell?.textLabel?.text = "Gender\t: " + \(localTableData.personGender?.toString())!
case 6:
cell?.textLabel?.text = "Height\t: \((localTableData.personHeight)!)"
case 7:
cell?.textLabel?.text = "Weight\t: \((localTableData.personWeight)!)"
default:
print("error")
}
cell?.textLabel?.font = UIFont.boldSystemFont(ofSize: 12.0)
cell?.textLabel?.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
return cell!
}
//Inits --------------------------------------------------
override public init(frame: CGRect)
{
super.init(frame: frame)
self.setup()
}
required public init?(coder aDecoder: NSCoder)
{
super.init(coder: aDecoder)
self.setup()
}
override open func layoutSubviews()
{
super.layoutSubviews()
}
}
/** This extension adds the function to load data
- Author: Arun Patwardhan
- Version: 1.1
*/
extension UIVIdentityCard
{
/**
This function loads the data for the view
- important: This function does not do validation
- parameter newPerson: This is the object representing the person whose information will be displayed on the screen.
- requires: iOS 11 or later
- Since: iOS 11
- author: Arun Patwardhan
- copyright: Copyright (c) Amaranthine 2015
- version: 1.0
*/
@available(iOS, introduced: 11.0, message: "load data")
public func load(data newPerson : PersonDetailsModel)
{
self.localTableData = newPerson
self.personIcon.image = localTableData.personIcon
self.personName.text = localTableData.personName
self.personDetails.reloadData()
}
}
Add the placeholder image for the image view.
Select any of the simulators from the list.
Press โ + B to build the project.
From the Project navigator select the Framework file.
Control click and select “Show in Finder”.
Copy the framework to the “Desktop”.
We are done creating the reusable framework. We will not shift our focus towards testing this framework.
Using the Framework in a project
Let us now test the framework we created. We will do this by incorporating the code in our iOS App.
Create a new project. Call it “IdentityCardTest”.
Save the file in a folder of your choice.
Select the Project file and Embed the Framework into your project.
Add an image to your project, this will be the image that will be displayed in your custom view.
Switch to the Main.storyboard file. Drag a UIView into the ViewControllers view.
Set its identity to the UIVIdentityCard in the identity inspector. Also set its module to UIVIdentityCard.
Create an IBOutlet for this custom view.
Switch to the ViewController.swift file. Import the UIVIdentityCard framework at the top of the file.
Add the following code to the file. We will be creating test data and displaying it on the screen using the Custom view we just designed.
//Functions --------------------------------------------------
/**
This function prepares and loads the data that is to be shown in the custom view
- important: This function does not do validation
- requires: iOS 11 or later, the UIVIdentityCard framework.
- Since: iOS 11
- author: Arun Patwardhan
- copyright: Copyright (c) Amaranthine 2015
- version: 1.0
*/
@available(iOS, introduced: 11.0, message: "prepares data to be shown on the ID card")
func prepareIDCard()
{
let displayData : PersonDetailsModel = PersonDetailsModel(withName: "Arun Patwardhan", icon: UIImage(named: "iconHolder.png")!, birthday: Date(timeIntervalSince1970: 44_97_12_000), address: "Mumbai, Maharashtra, India", phone: "91-22-26486461", email: "arun@amaranthine.co.in", Company: "Amaranthine", height: 5.11, weight: nil, andGender: GenderType.Male)
myIDCard.load(data: displayData)
}
Your completed ViewController.swift should look like this.
Run the project. See if the view loads the way we wish.
An important aspect of programming, and one that people don’t think of to often, is being able to express the intentions of the code clearly.
Most of the times we programmers get lost in the code we write. It is important to step back and take a look at the code we have written from another person’s perspective. One can say, “But that’s what documentation is supposed to do right? Provide information to others!”. Yes, but that’s not the only way. A good example of that is a situation we often face with functions.
Command Query Separation
Most functions can be generalised into 2 categories.
Command Functions
Functions that act on instructions sent to it and make changes to the underlying data/model. These are commands given to a function and the callee is not expecting a response.
Query Functions
Functions that are used as queries to examine the underlying data/model. The callee is most certainly expecting a response. The function should not modify the underlying model in any way.
It is not common to find a function that does both. In fact, to be consistent command functions must never return a response and a query function must only return a response. This is how ideal separation happens. This way programmers can easily distinguish between Commands & Queries and the objective of the function becomes clear.
The real world however is quite different. Most functions we write are not guaranteed to work the way we want. The likelyhood of an error occurring while a function is being run is very high. This can happen during data validation or some underlying process. Hence, most functions are very likely to return a response indicating the success of a function. This is done using a variety of techniques. It is this feature that throws Command Query Separation for a toss.
In this article we are going to look at some ways in which we can achieve Command Query Separation while still retaining error handling capabilities.
Let us start by looking at the example written below.
//Division function
/*
Argument 1: Holds the numerator of type double
Argument 2: Holds the denominator of type double
Returns: Value of type double. The result of the division is returned. If the denominator is 0 the function returns 0
*/
double division(const double &firstNumber, const double &secondNumber)
{
if (!floating_point_equality(firstNumber, secondNumber))
{
return firstNumber / secondNumber;
}
return 0.0;
}
The function is a rather simple implementation of division written in C++. It is meant to be a Query function. It immediately becomes clear that we are trying to do 2 things here:
We are trying to perform a division
We are trying to check if the division succeeded with the help of a return value
The problem is the fact that the function returns error codes and the result the same way. Any programmer using this function will have to write the code to distinguish between the two.
In this case the error is represented by the value ‘0’. There is no way for the caller to tell if the result of the division was 0 or if there was an error. It gets even worse if the function is a pure command function. A pure command function ideally should not return anything. However we will have to return a value to account for errors.
Here is an example of a Command Function:
//Division function
/*
Argument 1: Holds the numerator of type double
Argument 2: Holds the denominator of type double
Returns: An error code in the form of an integer. A '0' indicates success. '-1' indicates division by Zero error.
*/
int display_division_of_numbers(const double &firstNumber, const double &secondNumber)
{
if (!floating_point_equality(secondNumber, ZERO))
{
std::cout<<firstNumber<<" divided by "<<secondNumber<<" = "<<(firstNumber / secondNumber)<<span id="mce_SELREST_start" style="overflow:hidden;line-height:0;"></span><std::endl;
return 0;
}
else
{
return -1;
}
}
As we can see the function is a command function. It shouldn't be returning a value. But we are forced to return a value to communicate success.
Let us look at some alternatives.
Returning Error Codes
This approach is the one that was implemented above & has the obvious short comings.
Passing An Error Code variable
This is the next best approach. Instead of returning an error code pass in an object that represents error. After the call is completed, check to see if the error object is nil/NULL. This ensures that the return value always corresponds to an answer and nothing else.
//Potential Error Codes
typedef enum ErrorCodes
{
DIVIDE_BY_ZERO, NaN, NEGATIVE_NUMBER
} ErrorCodes;
//ErrorCode struct. This is the object that contains error information
typedef struct ErrorCode
{
public:
ErrorCode(const ErrorCodes &code, const std::string &description)
:errCode(code), errDescription(description)
{
}
std::string description() const
{
return errDescription;
}
private:
//Holds the code
ErrorCodes errCode;
//holds additional information
std::string errDescription;
} ErrorCode;
//Division function
/*
Argument 1: Holds the numerator of type double
Argument 2: Holds the denominator of type double
Argument 3: Holds a pointer to the error code object. If the object is nil then there was no error.
Returns: Value of type double. The result of the division is returned.
*/
double division_of_numbers(const double &firstNumber, const double &secondNumber, ErrorCode **error)
{
if (!floating_point_equality(secondNumber, ZERO))
{
return firstNumber / secondNumber;
}
else
{
*error = new ErrorCode(DIVIDE_BY_ZERO, "Attempting to divide by zero");
}
return 0.0;
}
As is obvious from the code above, the return value always corresponds to the answer of the computation. All we have to do is check theย ErrorCode pointer to see if it is NULL.
Here is the implementation for the Command Function.
//Division function
/*
Argument 1: Holds the numerator of type double
Argument 2: Holds the denominator of type double
Argument 3: Holds a pointer to the error code object. If the object is nil then there was no error.
*/
void display_division_of_numbers(const double &firstNumber, const double &secondNumber, ErrorCode **err = NULL)
{
if (!floating_point_equality(secondNumber, ZERO))
{
std::cout<<firstNumber<<" divided by "<<secondNumber<<" = "<<(firstNumber / secondNumber)<<std::endl;
}
else
{
*err = new ErrorCode(DIVIDE_BY_ZERO, "Attempting to divide by Zero.");
}
}
As you can see the function looks like a true Command Function. There is no value being returned. However, the caller still has to check if the Error object is NULL.
Another implementation of this is to use a complex response.
//Potential Error Codes
typedef enum ErrorCodes
{
DIVIDE_BY_ZERO, NaN, NEGATIVE_NUMBER, NO_ERROR
} ErrorCodes;
//Response struct. It will hold either the error or a response.
typedef struct Response
{
public:
Response(ErrorCodes err)
: errCode(err), value(0.0)
{
}
Response(double answer)
: errCode(NO_ERROR), value(answer)
{
}
ErrorCodes getError() const
{
return errCode;
}
double getValue() const
{
if (NO_ERROR == errCode)
{
return value;
}
return 0.0;
}
private:
ErrorCodes errCode;
double value;
} Response;
//Division function
/*
Argument 1: Holds the numerator of type double
Argument 2: Holds the denominator of type double
Returns: A struct of type Response that either contains the value or the error. The caller must examine the struct before probing the value.
*/
Response* division_of_numbers(const double &firstNumber, const double &secondNumber)
{
if (!floating_point_equality(secondNumber, ZERO))
{
Response *answer = new Response(firstNumber/secondNumber);
return answer;
}
else
{
Response *error = new Response(DIVIDE_BY_ZERO);
return error;
}
}
This approach is a combination of the first 2 approaches. It immediately sends information to the caller that he/she must examine the object for errors before probing for the value. In the earlier example, there is no guarantee that the caller will examine the error. There is no guarantee with this approach either. But at least it simplifies the implementation for the caller and provides an easier mechanism to handle errors without having to manually create error objects.
Something similar is achieved in Swift using Associated Enums.
//Response Enum. It will hold either the error or a response.
enum Response
{
case Error(String)
case Value(Double)
}
//Division function
/*
Argument 1: Holds the numerator of type double
Argument 2: Holds the denominator of type double
Returns: An Enum Response that either contains the value or the error. The caller must examine the struct before probing the value.
*/
func division_of_numbers(firstNumber : Double, by secondNumber : Double) -> Response
{
if (!floating_point_equality(firstNumber : secondNumber, Equals: ZERO))
{
let answer : Response = Response.Value(firstNumber/secondNumber)
return answer;
}
else
{
let error : Response = Response.Error("Dividing by Zero")
return error;
}
}
Of course the Swift implementation does not need a struct as enums allow us to encapsulate a value in them.
Exceptions & Exception Handling
This is a much better approach. The idea is that you write you function to work as it is normally supposed to. If something goes wrong throw an exception. This approach completely eliminates the need to examine the return value or check to see if there are errors in the response object.
In exception based programming, your code will follow the correct path if there is no problem. If an issue occurs then your code jumps to the part where the error needs to be handled.
#include <span id="mce_SELREST_start" style="overflow:hidden;line-height:0;"></span>
#include
#include "MathException.hpp"
const double ZERO = 0.0;
//Floating point equality checker
/*
Argument 1: Holds the LHS value of type double
Argument 2: Holds the RHS value of type double
Returns: Boolean value
*/
bool floating_point_equality(const double &firstNumber, const double &secondNumber)
{
return fabs(firstNumber - secondNumber) < std::numeric_limits::epsilon();
}
//Division function
/*
Argument 1: Holds the numerator of type double
Argument 2: Holds the denominator of type double
Returns: A struct of type Response that contains the value.
This function throws an exception of type MathematicalExceptions::MathException
*/
double division_of_numbers(const double &firstNumber, const double &secondNumber)
{
if (!floating_point_equality(secondNumber, ZERO))
{
double answer = firstNumber / secondNumber;
return answer;
}
else
{
MathematicalExpections::MathException exception = MathematicalExceptions::MathException("Attempting to divide by zero");
throw exception;
}
}
int main(int argc, const char * argv[]) {
double numerator = 32.1;
double denominator = 0.0;
double answer = 0.0;
try
{
answer = division_of_numbers(numerator, denominator);
}
catch (MathematicalExceptions::MathException &err)
{
std::cout<<err.what()<<std::endl;
}
return 0;
}
Exceptions are about the closest we can come to achieving Command Query Separation. Anyone using functions that implement the Exception throwing and handling capability is clear as to whether it is a Command function or a query function without compromising on safety and error handling in any way.
Here is an example with Swift.
//Exception Enum. Will be used to throw an exception for mathematical operations
enum MathExceptions : Error
{
case Divide_by_Zero(String)
case NaN(String)
case NegativeNumber(String)
}
//Division function
/*
Argument 1: Holds the numerator of type double
Argument 2: Holds the denominator of type double
Returns: The value of type Double. The caller must handle any exceptions that might be thrown.
*/
func division_of_numbers(firstNumber : Double, by secondNumber : Double) throws -> Double
{
if (!floating_point_equality(firstNumber : secondNumber, Equals: ZERO))
{
let answer : Double = firstNumber / secondNumber
return answer;
}
else
{
throw MathExceptions.Divide_by_Zero("Attempting to Divide by zero.")
}
}
let ans : Double = 0.0
do
{
ans = try division_of_numbers(firstNumber: 22.3, by: 0.0)
}
catch let err
{
print(err.localizedDescription)
}
Again, the Swift implementation is rather Straightforward thanks to Associated Enums which conform to the Error protocol.
Here is how the Command function would look with exceptions.
//Division function
/*
Argument 1: Holds the numerator of type double
Argument 2: Holds the denominator of type double
*/
void display_division_of_numbers(const double &firstNumber, const double &secondNumber)
{
if (!floating_point_equality(secondNumber, ZERO))
{
std::cout<<firstNumber<<" divided by "<<secondNumber<<" = "<<(firstNumber / secondNumber)<<span id="mce_SELREST_start" style="overflow:hidden;line-height:0;"></span><std::endl;
}
else
{
MathematicalExceptions::MathException exception = MathematicalExpections::MathException("Attempting to divide by zero");
throw exception;
}
}
This produces a much better implementation of the function, while maintaining the error handling capabilities.
Conclusion
As we can see implementing perfect Command Query Separation is not easy. But by writing our functions properly and by using better error handling such as exceptions it becomes a lot easier to achieve that. Programmers should be able to look at a function & tell if it is a ‘Command’ or a ‘Query’ knowing that error handling is not part of the function signature in any way.