Print Hello World in Swift
Swift is a new programming language developed by Apple to develop MacOS and iOS apps. It was designed to be easier to learn as compared to Objective C and Apple sees it as a way to make it easier to get into app development for the MacOS and iOS platform as Swift was designed from the ground up to be an easier but powerful language to learn than the C like Objective C language.
Creating Apps versus Writing a Program
While generally you can’t create Mac OS or iOS apps without knowing how to program, you won’t necessary be able to create apps even after learning Swift. Swift is a programming language with standard inputs and standard outputs, namely keyboard, screen display, and the file system. The programs you will write in Swift will display in a console environment accepting input from the keyboard, and can read or write to files on the local hard disk.
To write Mac OS applications with nice user interfaces, we need to make use of the Cocoa API library.
To write iOS apps with nice user interfaces and touch input, we need to make use of the Cocoa Touch API library.
API stands for Application Programming Interface. It is basically a definition of application building blocks for developers to use to write specific types of applications quickly. The building blocks includes functions, data structures, object classes and variables.
A Software Development Kit (SDK) for a particular API needs to be downloaded to your development environment so that you can make use of the APIs. Currently the Mac OS and iOS SDKs are compatible with Objective C and Swift.
When you download and install the latest Swift, the latest SDKs for both the iOS and Mac OS are installed with Swift.
One of the more common reasons why people want to learn how to program is to be able to write their own games.
To be able to write games, obviously you need to learn how to program, and then on the Mac OS or iOS platform, you will need to learn how to make use of the Cocoa and Cocoa Touch library respectively and use the available graphical and animation functions to help write your game.
However, to make it slightly easier to write games, there are many game engines out there available for Mac OS and iOS that you can use so that you don’t have to re-invent the wheel when it comes to adding sounds, doing animations, or doing collision detections.
An example of a game engine is Cocos2d (http://www.cocos2d-x.org). You will need to download its SDK and learn how to make use of its API to be able to make use of its functionality. Cocos2d is a cross platform game engine.
There are many open source and commercial game engines which can be used.
Apple provides SpriteKit, which is a graphics rendering and animation infrastructure.
Xcode is the name of the development environment for Mac OS and iOS apps. It currently only runs on MacOS. This can be downloaded from the App Store. This needs to be installed first before we can begin programming in Swift.
After you have installed Xcode, you can launch it just to check that it’s working. Since Swift is constantly evolving because it’s a new language , be sure to check the App store regularly to update Xcode.
For now, exit out of Xcode as we are going to be using REPL.
REPL stands for Read-Eval-Print-Loop. It is a command line interactive environment that you can use Swift commands. This is an interpreted environment meaning each Swift command gets actioned immediately much like the old BASIC command line interpreters and Python command environments. This provides a good way of getting immediate feedback on Swift commands and very useful for beginners when first learning the language.
Once Xcode have been installed, REPL can now be launched from the Mac Terminal app.
To launch the Mac Terminal app, navigate to the Applications, Utilities folder. Double-click on the Terminal app.
Ensure you are using REPL with the Latest Swift version installed
At the Terminal prompt, type in the following to ensure that you select the latest Swift version installed to be used. As you update Xcode from time to time, new versions of Swift get installed. The following command ensures that REPL will use the latest Swift version installed. You would only type the following once. The only other time you would need to type it in again is if you have updated Xcode.
sudo xcode-select –s /Applications/Xcode.app/Contents/Developer/
It will prompt for a password. This is the administrator password to your Mac.
Running the REPL environment
If you are prompted to enter a password, just type in the administrator password to your Mac.
What you should now see at the end of the messages that may have scrolled past is:
Welcome to Apple Swift version 2.2 (swiftlang-703.0.18.8 clang-703.0.30). Type :help for assistance.
This is the REPL environment. We’re currently using Swift version 2.2.
Type in :help at the 1> prompt and hit Enter.
This will list all the commands available to use in the REPL environment
To exit out of the REPL mode, we would just need to type in :exit or :quit and hit Enter.
Print “Hello World”
At the beginning of learning a new programming language, it is customary to use the language to print Hello World to the standard output.
At the 1> prompt, type in print (“Hello World”) and hit Enter.
1> print("Hello World")
print is a Swift function which accepts a parameter and displays it to the default output.
Swift functions make use of the braces () to accept their parameters. In this case, the “Hello World” message was passed to print inside the braces.
Printing Hello World in Swift in REPL or as a console application is relatively simple. My next tutorial shows how to print Hello World to the iPhone using Swift.
More by this Author
How To Setup A Remote Desktop Gateway. What is Remote Desktop Gateway and how to install.
This hub provides a guide for resizing a Linux file system running under the Logical Volume Manager (LVM). These steps WILL NOT work for a file system that is not running under the LVM.