Last updated: Aug 3, 2016 by Raja Tamil
Swift is Apple’s new programming language and was released in 2014. Swift is the language for building Mac OSX, iOS, WatchOS and tvOS apps. Swift is C-style language so if you have any experience with any C-style languages such as C, C++, Objective-C etc., you will be at home learning swift.
In this post, you will learn:
• Why you should use Swift
• Getting Started with Constants and Variables
• String Interpolation
I highly recommend using Swift to build apps in Apple OS platforms because Apple has been rewriting some of their apps partially or fully in Swift. Swift 3 beta was released at the WWDC in 2016 and there were some major changes implemented. We expect to see more changes as it becomes more stable in the upcoming months. If you just started learning Swift, you will not have to worry about the different Swift versions. You can dive right into learning Swift without worrying about Swift Evolution. A lot of people begin to learn Swift instead of Objective-C because Swift is very simple, easy to learn, expressive, concise, fast and type-safe language.
Constants and Variables
let yourBirthYear:Int = 1980
You use the
let keyword to declare constants (immutable or unchangeable) followed by the name of the constant (yourBirthYear) and it’s data type (Int) and assigning an initial value after the equal sign, in this case, 1980. As you can see from the example below, when you try to change the value of the constant, the compiler throws an error.
yourBirthYear = 1984 // swift throws an error
On the other hand, you use the
var keyword to declare variables (mutable or changeable) followed by the name of the variable (yourName) and it’s data type, in this case, “String”, then you assign the value to the variable. See the example below:
var yourName:String = "Ram" yourName = "Ramu"
Note: Unlike other languages, Swift does not need a semicolon at the end of a statement.
1. The variable or constant name is fully Unicode friendly, you can use special characters like the code below:
let π = 3.14 let 🐶 = "dog"
2. You can write multiple constants and variables in a single line.
var red = 120.0, name = "Ram", age = 35
In the above example, I do not have to include the type of variable. The reason is, Swift will infer the data type of variable for us behind the scene. Swift infers variables or constants data types based on their values. Swift Infers the variable “red” as a Float, “name” as a String and age as an Int. The reason you would always want to declare a value when you declare constants or variables is so that you do not have to type their data types. This will make our code very clean and easy to read.
3. Printing constants and variables.
You use the print function (global function) that will output one or more values in the Xcode’s console pane.
var message = "Hello World!" print(message) // Hello World!
When you make a long string and you want to inject the variable or constant inside. You use a variable or constant name in the place where it’s content would appear, wrapped with parenthesis backslash in front of it. See the example below:
var firstName = "Ramu" var age = 22 Print("My Name is \(firstName) and my age is \(age)"); // My name is Ramu and my age is 22
In this post, you have been exposed to why you would want to consider to learn Swift, Constants, Variables and String Interpolation.
Leave a comment if you have any questions or suggestions. If you like it, don’t forget to share it with your friends. Thanks for reading!