Class VS Struct -Swift
// Tip From Writer“Command + /” is beautiful combination when wanna make comment line(s).
What is difference between Class and Struct in Swift and how to choose which one is correct for you! In swift , there is almost no difference between classes and structs. Well then why there are 2 options that can do the same ?
Because even small differences between them will affect our application a lot. When choosing class or struct. To understand them, we will have to look at the commonalities and especially the differences between them.
First of all, let’s start with the common uses of classes and structs.
- Classes and structs can conform to protocols (Protocol Oriented Programming)
- Classes and structs can extended with extension
Also Structs suggested in swift documentation why ? We should focus on the differences between Classes and Structs.
- Classes can inherit from another class while structs cannot. (Can take once)
- In addition, on Structs initializer is automatically created by swift , also on Classes it is necessary to write it manually.
- The most important difference between classes and structs, Classes are reference type and Structs are value type.
Also “Reference type and Value type” is big topic , i did wrote my next article about it .
So should we use classes or structs ? Generally if objective-c frameworks/functions will be used class recommending .
I usually prefer to use struct because of swift documentation and what I have explained. I hope this article was helpful.
References :