As we know classes are reference type in Swift programming. Here I only discuss classes as a data type and will explain more in Swift Object Oriented part.
In object-oriented programming, a class is an extensible program-code-template for creating objects, providing initial values for state (member variables) and implementations of behaviour (member functions, methods). (Wikipedia)
Before starting a code, I am describing what classes can do and what not in Swift.
- Swift class support only single level inheritance
- Swift class support Polymorphism
- Swift class support three Access modifiers internal, private and public.
- Swift class support class level methods and properties
- Swift class support instance level methods and properties
All above points will discuss with detail in Swift Object Oriented section.
As we mostly start Object Oriented Programming, we define a class Person. Same thing happen here 🙂 .
Instance method and calling.
Class level method and calling.
In most languages we use static keyword for class level methods and fields. But in swift we use the above image syntax to define a class level methods.
Getter and Setter Method.
In above image, mFirstName is an instance level field having getter and setter. Here only I am showing syntax of Getter and Setter method, otherwise we did not require these methods in this scenario, because we can access this field using dot operator.
Class level field.
Pass By Reference.
Basic’s of class complete. Now we are moving to struct and I will explain classes in more detail when we start Object Oriented Programming in Swift.
In Swift structures are value type. In first look structures and classes look like similar but there are some differences.
- Both have properties
- Both have methods
- Both have init methods
- Both use extensions and protocol (Will explain in next section)
- Structure are not inheritable
- Structures cannot achieve polymorphic behaviour
- Structures cannot support De-initializer method classes can (Explain in OOP in Swift section)
Now Structure of a Person 🙂
As we know and will learn more in future about classes require init method but in struct they are optional.
As you see now we did not define init method. In this case swift automatically generate init method for us. This is called member-wise initializer.
If we want to initialize our struct without any argument then we need give default values to struct fields as shown below image.
We can define methods in struct as in classes.
Struct level method and calling.
As we see in class, we use the keyword class to make a method as class level. But in struct we use static keyword to make a method as struct level.
Pass by value.
As you see in above image. When we change p1 age field p2 age field not effected. But when we did same thing in classes our p2 age field also effected. So this is the one big difference between struct and classes. Structs are value type and classes are reference type.
One more important thing in Swift. Every data type which we use in Swift like Int, String, Float, Arrays, Dictionaries etc all are Structs 🙂 .
Hmmmmmmm. OK Guys we will meet in next tutorial Bye. 🙂