Welcome back! The objective of this tutorial is, to learn how to create a swift project, compile it and run it successfully.

Method 1 – Using Xcode:

To write a Swift program in Xcode is fairly straightforward.

  • First open up Xcode
  • When it opens you will see a following window with some options, here select ‘Get Started With a Playground’.

Katpara Enterprise - Swift Programming - Hello World

  • In the next screen, you can give the name as you desire, and leave the platform dropdown default ( here it’s iOs ), then click ‘Next’, and select the directory where you want to save the project.

Katpara Enterprise - Swift Programming - Hello World

  • Finally, you will see the editor window, with the pre-generated source code.

Katpara Enterprise - Swift Programming - Hello World

The window above has 2 main sections, the white area on the left is your editor to write your code, and the gray area on the right is the output window, that shows the output of your code by each line.

Although I won’t be using Xcode, you are more than welcome to use it, if it is convenient.

Method 2 – Using Terminal:

If you don’t have Xcode, you can follow the steps below.

  • First navigate to any directory, where you can save your code files ( I chose home directory ).
$ cd ~
  • Create a directory called ‘swift’, where the ‘swift’ directory will contain all the swift projects, and go into that.
$ mkdir swift
$ cd swift

Now, let’s create the first project called helloworld’!

  • In order to do that, create a directory called ‘helloworld’ inside the ‘swift’ directory.
$ mkdir helloworld
  • After that go into the ‘helloworld’ directory and type the following command.
$ swift package init --type executable

This will generate a list of files and directories as shown below,

-- .gitignore
-- Package.swift
-- Sources/
|  |
|  ---- main.swift
-- Test/
  1. .gitignore – The file used for GIT version control system.
  2. Package.swift – The manifest file of the project. This must be present in the project root.
  3. Source/ – The directory is used to put the project source files.
  4. main.swift – The file is the main entry point of the application.
  5. Test/ – The directory is used to put the project test files.

After opening the main.swift file in any plain text editor, you will see the pre-generated code.

print("Hello, world!")

woohoo! Let’s run the code!

In order to run the code, first it need to be compiled, that will generate an executable. In order to do that simply type following command in the project root (in the ‘helloworld’ directory).

$ swift build

The output should show the compiling and the linking of the project.

Compile Swift Module 'helloworld' (1 sources)
Linking ./.build/debug/helloworld

Now to run your code, just type the path that is given at the ‘Linking…’ statement in the output above.

$ ./.build/debug/helloworld
Hello, World!

For this series of tutorials, I would recommend to use the Terminal, with a plain text editor such as Sublime Text!


In this tutorial, we learned how to create a Swift project, how to compile the project, and at last how to run it. We learned the directory structure of the Swift package, the importance of the Package.swift file and main.swift file.

In the next tutorial we will learn about variables and constants. Please click here to read.