Introduction to Swift
Swift is a programming language introduced by Apple in June of 2014 at the Worldwide Developers Conference (WWDC) which took place in San Francisco. Swift was initially Apple's proprietary language, but on December 3rd, 2015, it was made open-source under the Apache License 2.0.
To use Swift for iPhone app development, you need Xcode 6 or higher. As of writing this post, the latest stable version of Xcode is 15 which was released in 2023.
You can also use Swift as a general purpose programming language outside of iPhone app development.
There are many ways to get started with Swift. The website swift.org contains all the documentation about Swift as well as guides and examples to get you started. Apple's Swift Playgrounds is a great resource, especially if you are new to programming, that runs on your Mac or iPad. You can also run Swift programs in the command line which is what I will demonstrate.
Create Your First Swift Program
Install Swift
The easiest way to get Swift installed on your computer is to install Xcode. There are other options if you aren't using a Mac or don't want to install Xcode. If you are interested in using Swift to develop iPhone apps you'll need to install Xcode anyway so I recommend installing it now. You can download the latest version of Xcode in the App Store. It is free to download but only available for Mac.
Open the Command Line
Open the command line. On a Mac you can press the command and space bar buttons to open Spotlight Search and then type "terminal". The top result should be the Terminal application which is the application to use the command line on a Mac.
In the command line you can enter the "pwd" command to see your current working directory. If you would like to save your program in a different directory, you can use the "cd" command followed by the path of the directory where you would like to go. You can also create a new directory by using the "mkdir" command followed by a space and the name of the folder to create.
Write Your First Swift Program
echo "print(\"Hello, World\!\")" >> hello.swift
This creates a file named "hello.swift" with one line of code:
print("Hello, World!")
Compile Your Code
In the command line, use the "ls" command to list the files. You should see "hello.swift" listed. Your Swift code can't be run until it is compiled. The complier should have been installed when you installed Swift. You can use the command "swiftc" followed by a space and the name of a Swift file in the command line to compile Swift code. Type the code below in command line:
swiftc hello.swift
If you run the command "ls" again, you should see a new file called "hello" without the ".swift" extension. This is your compiled Swift program also called an executable file.
Run Your Code
You can run your code by typing "./" then the name of your Swift program in the command line. Type the code below to run your program:
./hello
You should see "Hello, World!" printed in the command line. Congratulations! You just created your first Swift program!
Comments
Post a Comment