Free Shipping

Secure Payment

easy returns

24/7 support

  • Home
  • Blog
  • The Swifty Way ! – Introduction to Swift Programming – Part 1

The Swifty Way ! – Introduction to Swift Programming – Part 1

 July 20  | 0 Comments

The situation when there is no value for a variable in swift is handled via the concept of Optional. By marking a variable as Optional we define a variable wherein the variable can have value or the variable doesn’t have a value at all.

A variable defined as a ‘var’ or a ‘let’ in previous section are all non optional type, ie., these type will never be having nil value in their lifetime.

Let have a look into some swift code that which is used to define optional

var someOptionalIntValue:Int?
var someOptionalStringValue:String!

The above two variable are marked optional. The “?”(question mark) or the “!”(exclamation) at the end of the type would mark a value to be an optional.

Difference between the ? and ! is best understood by getting to know about unwrapping process.

The value of the optional marked with “?”(question mark) cannot be directly used. The situation of a value marked as optional can be compared to an item stored inside a container and in order to use this item we need to open the container.

However in the second case in which the optionals marked with “!”(exclamation) are the ones that are called explicitly unwrapped optional. Unlike the optional marked with “?” the value of these optional can be directly accessed. One need to be vigilant while using “!”, as in the case when the optional value is nil and if developer access the value, a runtime error will occur.

If we try to assign a nil to a non-optional value a compile time error will be generated. The following code demonstrates the same.
var myExampleVariable = 52
myExampleVariable = nil //INVALID This line will show compilation error stating that Nil cannot be assigned to type Int (playground xcode 7.3)

The above situation cane be handled by using optional value.
var someOptionalDoubleValue:Double? = 52
someOptionalDoubleValue = nil; // Assigning nil to an optional value is valid

In the case when a optional value is assigned to a non-optional type then the optional value need to be unwarapped.
By now you might have noticed that none of the optional values are declared with the “let” type in above examples.
What do you think will there be a case that would lead a developer to declare optional let type that which is immutable ? Think about it!

Unwrapping the Optionals

Unwrapping is process of accessing the value from an optional type. The following code will give us a notion on the unwrapping process of optional values.

a. Declaring a optional variable
var someOptionalStringValueWithQuestionMark:String?

b. Assigning a value to the variable
someOptionalStringValueWithQuestionMark = “hello world”

c. Printing the variable
print(someOptionalStringValueWithQuestionMark) // Optional(“hello world”) will be the out put

d. Using the “!”(exclamation) operator(also know as bang operator in the current context) at the end of variable to access the value from the variable(The unwrapping of optional variable happens)

print(someOptionalStringValueWithQuestionMark!) // hello world will be the out put here

e. Assigning nil value and trying to unwrap and print the value

someOptionalStringValueWithQuestionMark = nil
print(someOptionalStringValueWithQuestionMark!) // This line will produce a runtime error

The reason for the crash is obvious in this case and by now the reason for the runtime error might have struck your mind.

However for implicitly unwrapped optional the unwrapping is not necessary and incase we use the bang operator here the value of the variable will still be returned. The following code is an example for the same

a. Declaring a implicitly unwrapped variable

var someOptionalStringValueInplicitlyUnwrapped:String!

b. Assigning a value to the variable

someOptionalStringValueInplicitlyUnwrapped = “hello world”

c. Printing the variable

print(someOptionalStringValueInplicitlyUnwrapped) // “hello world” will be the out put

d. Use bang operator also will give the “hello world” as output


Class and Structs

The only difference between the two is that classes are reference type and structs are value type.

While a struct object instance is used the value is copied and this behavior can be noticed in the example shown below. The struct object instances aPoint, bPoint and cPoint are all having different value and is not mutated while it is assigned, passed to a function parameter or returned from a function.

Where as in case of the class object instance being reference type all the variable holding the same reference gets mutated and the behavior is noticed with all the three variable aPoint, bPoint and cPoint.
Classes and structs can have methods. The getAnAribitararyPoint is an example of the method.
The following shows how the class and struct could be declared.

Class sample

Struct Sample


Use of Class, Static, Final keywords before methods

The methods can be marked final with final keyword . It is not possible to override there methods in subclasses.

Methods can be marked as static as well and the behavior of static methods is equivalent to final methods.

Another kind are the class methods. The only difference with the final/static being the fact that overriding the implementation in the subclasses is possible with function marked with class keyword.
Now that its time to conclude this blog . Hope you have enjoyed reading !

Even though we conclude The Swifty Way ! – Introduction to Swift Programming Part 1 here, the cool features of swift does not stop with these. There are lots more !!
Watch this space for more and in The Swifty Way ! – Introduction to Swift Programming Part 2 we will continue our exploration…