Learning Swift Series: Part 1 – Basics

In part one of Learning Swift Series, we will deal with the following topics that cover basics of Swift language:

  1. Constants and Variables
  2. Data types
  3. Type safety and Type Inference
  4. Type annotation
  5. Type alias
  6. Comments
  7. Semicolon
  8. Assertions and Preconditions

Type annotation in Swift

Type Annotation is used to provide information about the type of values stored by the constants and variables. Mostly we don’t have to use type annotation as Swift can infer the type of values stored in its variables and constants.

Example:

let textTwo: String = “Come let us learn Swift”

In the above code, constant textTwo is of type String.

Type annotation is written by placing a colon after the name, followed by a space and then the type of value stored by the variable/constant.Type of constant and variable cannot be changed once they are declared.

Return from initializer without initializing all stored properties error in Swift initialization

This error may occur due to the fact that a constant is assigned a optional type and not provided with explicit value. Constants can be assigned with a value only once. So when we declare a stored property inside a type as constant with optional type, Swift compiler wants us to assign a explicit value during initialization as the value cannot be overwritten in future. We can either provide a optional constant with nil  or provide any default value. Once it is assigned with a explicit value we cannot change the value for the optional constant after a instance is initialised. This is not true for variable type as it is automatically assigned with nil value during initialization and we can change that value after initialization.

 

 

 

Why we cannot modify stored properties of constant Struct instances?

We cannot modify the properties (constant and variable stored properties) of struct instances assigned to a constant because, structures are value types. When a instance of a value type is marked as a constant , all of its properties will automatically get marked as constants and no modification is allowed.

Swift: Stored properties of constant struct instance

If we create instance of struct with a constant, we cannot modify its properties even if they are declared as variable stored properties.

Swift: Stored properties of variable struct instances

A stored property is a constant or variable that is stored as a part of an instance of a particular class or structure. Stored properties can be variable stored properties(var) or constant stored properties(let). We can set default value for a stored property as a part of its definition or set and modify the initial value during initialization for both constant and variable stored properties. Once values are set for constant stored properties they cannot be changed.

 

Constants and Variables in Swift

Constants and Variables associate a name with a value of particular type. They are like containers to store values.They must be declared before they are used.

  • Constants are declared using ‘let’ keyword.Values associated with a constant cannot be changed once they are assigned.
  • Variables are declared using ‘var’ keyword.Values associated with a variable can be changed after they are assigned.

Example:

var textOne = “Hello, playground”

Above statement says: Declare a variable named textOne and assign it with a value of “Hello, Playground”.

let textOne = “Hello, playground”

Above statement says: Declare a constant named textOne and assign it with a value of “Hello, Playground”.

  • We can declare multiple constants / variables of same type separated by commas in a single line using let/var only once.

Example:

var class10 = 24, class11 = 30, class12 = 25

  • Names associated with constants and variables cannot contain whitespace characters, private unicode characters, line-box drawing characters, mathematical symbols and arrows. Names must not begin with numbers but can have them elsewhere in the name.

Constants and Variables

  • We can print the current value of constant and variable using print function. Using string interpolation, the value of constant or variable can be included as a part of string by enclosing them within a parentheses followed by backslash  before the opening parentheses.

Example:

let counter = 35

print(” The counter value stored inside the constant counter is given by : \(counter)”)