Introduction
What are Classes?
- Blueprint for creating traditional “Objects”
- Instance of any type referred to as “Instances”
- Can have properties, methods and subscript
- Can inherit and adopt protocol
- Can have initializer and DE initializer
- Can be extended
- In class, there is implicit sharing of objects
- Data is mutable
- Slower memory allocation
Creating Classes
The structure and declaration of class is also the same in swift programming language. Now I am discussing the basic example of class -- now consider the following code,

Now I declare a simple class in which I declare two variable names as first name and last name and make a function which shows the full name but when I run it you see an error that “Class Employee has no initializers,” now the points in my definition of class will clear up your concept more.
Creating Classes with Initializers
In Swift programming language, we initialize our class with a different approach -- we have a built-in method in swift called “init” which performs the action called initialization.
Now we modified our previous code and checked with initializer that our code runs correctly.

Class is a Reference Type
- Basically when we deal with classes in your program we can’t store an actual value, we store reference
- A class stores a pointer to a location in memory that stores value
- We make multiple instances on one class.
Class Uses Heap Memory
- When we create a reference of class the system stores the actual value in a region of memory known as the heap.
- The heap is very large pool of memory from which the system can request and dynamically allocate the block of memory
- The heap doesn’t automatically destroy its object like our stack, because in stack when we exit the program the objects are released from the memory
- In heap memory you are fully responsible for both allocation and de-allocating.
- Creating and removing data from heap is a slower process than the stack memory.
Class Adopt Protocol
If you learn the concept of Protocol in swift programming language, the best advantage of protocol is that we inherit and adopt the protocol. Now I am again giving you an example which will clear up your concept about adoption of protocols.

In this Code example, I justify my point which I gave when I gave you a definition of class. Here you see that class adopts the protocol and we modified our example with protocol method which clears up your concept more.
DE initialization Class
Now we see how we DE initialization in the class means we assign at nil. In Swift programming language we have some built-in methods like “deinit” to DE initialization the class. Now I am discussing the example which will clear up your concept of DE initialization of classes.

In this example, we first declare two variables with name “firstname” and “lastname” and then we initialize both variables using “init” method and then immediately we use “deinit”.
Then outside the class we create nil instance the “?” in class which means we assign it “nil;” then I assigned two values and immediately assiged it “nil;” now after assigning “nil” our class is DE initialized.
Conclusion
This is all about classes in swift programming, I am guesing that you have now a clear picture about the classes, but if you have any confusion you can ask me I will try to clear up your concept.

Ravi KandelPosted Jul 14, 2016, 12:05 PM
Thanks for sharing.
RajaPosted Jul 8, 2016, 12:03 AM
Good One....
Guest UserPosted Jul 7, 2016, 8:08 PM
Can you please explain PROTOCOL and DE in detail? thanks for the article
Guest UserPosted Jul 7, 2016, 8:06 PM
There is managed & unmanaged code by the framework. Managed code includes static (stack), dynamic (heap) memory which is taken care by modern programming langs. The unmanaged code is something we need to take care of, e.g., when we OPEN a file or DB connection, it's done in C# by USING statement.
Guest UserPosted Jul 7, 2016, 8:05 PM
I think heap is also managed by modern programming languages like C# where we have Garbage Collector. I don't know from Swift perspective but I believe Swift shall manage heap too.
kalu singh raoPosted Jul 7, 2016, 2:20 PM
Nice...