Comparing Structures And Classes In Swift Language

Introduction

In this article, we are comparing the structure and classes in Swift language and I will try to make you understand the difference between the structures and class,  and the following chart enables you to understand the concept about the structure and class.

Comparing Structures and Classes in Swift

table

Key Point of Structure

  • Implicit copying of the values.
  • Data is immutable.
  • Useful for representing the values.
  • Fast memory allocation (stack).
  • It provides you the member wise declaration.

Member Wise Declaration

If we declare struct keyword, you can see that at the bottom, we make an object of struct. We have two suggestions -- one is simple, the declaration, and another is the member wise declaration. This declaration can be only held in struct.

code

Structure is a Value Type and Classes Are Reference Type, Is There Any Difference Between Coding and Result?

Structure

Yes, there are many differences between the coding and a result, I am discussing an example here, which helps to explain to you the concept clearly.

code

Code

  1. // Function that Changes Int  
  2. func changeValue(var number: Int) - > Int  
  3.     {  
  4.         number += 1000  
  5.         return number  
  6.     }  
  7.     // Defines an Int Variable(Value Type)  
  8. var myNumber = 99  
  9.     //Pass into the function Pass By Value  
  10. changeValue(myNumber)  
  11.     //Originally Variable is unchanged  
  12. myNumber  
Listen: First, we simply declare a function with the number 1000, followed by declaring a new variable and assigning it any number. I pass this variable into my function. You will see that my previous number is added in the function and see the sum result, but when I print the myNumber variable again, the value of it doesn’t change and remains the same.

Key Point of Classes

 

  • Implicit sharing of the objects
  • Data is immutable
  • Useful for representing the things
  • Slower memory allocation (heap)
  • Classes store the properties and methods
  • Classes are the references, that are shared on assignment.

Classes

Now, I will discuss the example code of the class, which is exactly the opposite of the structure.

code

In this example, we will declare the class name as checking and then we declare a variable. Assign a number and then we make a function. We make an instance of the class, which is passed to our function and then we print the instance. Now, you will see that the value changes, using the class reference.

Code

  1. //Pass By Refernce  
  2. class checking  
  3. {  
  4.     var value: Int = 99  
  5. }  
  6. func ChangedObject(var object: checking) - > Int  
  7.     {  
  8.         object.value += 1000  
  9.         return object.value  
  10.     }  
  11.     //Create a new instances(References Type)  
  12. var myObject = checking()  
  13.     //Pass the object into the function pass by Refernces  
  14. ChangedObject(myObject)  
  15.     //The Original Object has been changed  
  16. myObject.value  
Instance Declaration

If we make an instance of the class, we don’t have an option to declare a class as a member wise declaration. Here, you can see that we need to change the keyword struct to the class and the member wise declaration can’t appear, because the class gives this permission

code

Conclusion

In this article, I tried to explain the concept of struct and class. I also gave the conceptual difference between these two things, because the beginner developers and students confuse these two things. 


Similar Articles