Structures In Swift Programming Language

Introduction

In this article, we will learn the basic yet more powerful and efficient concept of Structures in Swift programming language

What is Structure?

  • Structure is a named type, like Int or Array which means you can define your own structures to create named type and later use it in your code
  • Structures are value type (Pass by Value). When assigned to another variable, or passed to a function, a structure's value is copied.
  • Structure allows us to encapsulate related properties and behaviors.
  • Structure allows us to declare member wise.

Structure Declaration

We can simply declare a structure with the struct keyword. Then, we give the name to the structure. After that, we use curly brackets to write the code inside it.

Code

Code

In this block of code, we declare a struct named as fullname in which I declare two variables named as firstname and lastname. After this, we can initiate our structure with member wise declaration. Now, we pass the two members which are used in above structure.

Initializers Rules

Initializers in structs have a few rules which need to be followed when we initialize the variables. The structs must have initial values set in all stored properties.

If we make one struct without initiating its variable, it will show an error at the time of compilation.


Code

Code
  1. struct fullname   
  2. {  
  3.     var firstName: String  
  4.     var lastName: String  
  5.     init(fName: String, lName: String)  
  6.     {  
  7.         firstName = fName  
  8.         lastName = lName  
  9.     }  
  10. }  
So, we solve this error by assigning the initialized variable with global variables  like this,

Code

Creating Mutating Structure

By default, the method of structure cannot have changed itself or its properties. Remember that we can’t assign immutable values to structure.

Code
  1. struct Counter   
  2. {  
  3.     var total = 0  
  4.     mutating func increment()   
  5.     {  
  6.         print("\(++total)")  
  7.     }  
  8.     mutating func reset()  
  9.     {  
  10.         self = Counter()  
  11.         print(total)  
  12.     }  
  13. }  
  14. var counter = Counter()  
  15. counter.increment()  
  16. print(counter)  
  17. counter.increment()  
  18. print(counter)  
  19. counter.reset()  
  20. print(counter)  
When we compile the above code, we see the result that we can use only the function keyword while creating a function into the structure. We use mutating the keyword to make a function immutable because the structure is mutable.

Code

Conclusion

This is all about Structures. Hopefully, you understand it now in a better way. This is the key concept of Swift programming language. I tried to give some powerful examples to  clear up your concept.  I hope you will be able to easily implement this code and use it in your program. If you have any confusion regarding this topic, I am available to solve your issue.

 


Similar Articles