#codeindia |swift basic tutorial||Difference between a struct and a class in Swift|

preview_player
Показать описание
Difference between a struct and a class in Swift.
* Value Type: Struct , Enum
you pass a structure [ or enum] around your program, what gets passes around is a copy of the structure. So modifications to structures don’t get shared.
benefits of value types
thread-safe not requiring any synchronization.
In Objective — C, everything subclasses from NSObject. NSString, NSArray etc are all reference types. On the other hand, swift is rich in value types. String, Array , Dictionary etc are all structs in swift, which is a value type. There are so many advantages in using a value type over a reference type.

Differences between Stack and Heap!
reference type instances are stored in heap and instances of a value type such as struct resides in a region of memory called stack
Stack is used for static memory allocation and Heap for dynamic memory allocation, both stored in the computer’s RAM .

How Reference and Value types are stored in memory ?
* Value Type — Get Stored on Stack Memory.
* Reference Type — Get Stored on Managed Heap Memory.

* Reference Type: Class
When you pass a class object around your program, you are actually passing a reference to that object, so different parts of your program can share and modify your object. 

What is a class
a class is a blueprint from which individual instances are created.

A class is a blueprint for creating objects , providing initial values for state  and implementations of behavior .
We define a class using the class keyword.

What is a structure
A struct is similar to a class in terms of definition and instance creation.
We define a struct using the struct keyword.

common factors between struct and class?
* Both define properties to store values
* Both define methods to provide functionality
* Both define subscripts to provide access to their values using subscript syntax
* Both define initializers to set up their initial state
* Both can be extended to expand their functionality beyond a default implementation
* Both Conform to protocols to provide standard functionality of a certain kind

Some additional capabilities that structures don’t have
Inheritance
Type casting 
Deinitializers
Reference counting
let detail1 = BookDetail(name: "codeindia1", price: 10, author: "author1")

Memberwise Initializers for Structure Types
use to initialize the member properties of new structure instances
Initial values for the properties of the new instance can be passed to the memberwise initializer by name:

Benefit of structure
Structs are much safer and bug-free, especially in a multithreaded environment. Swift value types are kept in the stack. In a process, each thread has its own stack space, so no other thread will be able to access your value type directly. Hence no race conditions, locks, deadlocks or any related thread synchronization complexity.
* Class does support Inheritance. Class is a reference type and is stored in the heap part of memory which makes a class comparatively slower than a struct in terms of performance. Unlike a class, a struct is created on the stack. So, it is faster to instantiate (and destroy) a struct than a class. Unless struct is a class member in which case it is allocated in heap, along with everything else.
* Value types do not need dynamic memory allocation or reference counting, both of which are expensive operations. At the same time methods on value types are dispatched statically. These create a huge advantage in favor of value types in terms of performance.
* Even though struct and enum don’t support inheritance, they are great for protocol-oriented programming. A subclass inherits all the required and unwanted functionalities from the superclass and is a bad programming practice. Better to use a struct with protocol-oriented programming concept which fixes the above-said issue.
*
Difference between a struct and a class in Swift.

*

Some additional capabilities that structures don’t have
Inheritance
Type casting 
Deinitializers
Reference counting
Memberwise Initializers for Structure Types
use to initialize the member properties of new structure instances
Initial values for the properties of the new instance can be passed to the memberwise initializer by name:

A class can inherit methods, properties, and other characteristics from another class. When one class inherits from another, the inheriting class is known as a subclass, and the class it inherits from is known as its superclass. 
Benefit of structure
Structs are much safer and bug-free, especially in a multithreaded environment. Swift value types are kept in the stack. In a process, each thread has its own stack space, so no other thread will be able to access your value type directly. Hence no race conditions, locks, deadlocks or any related thread synchronization complexity.
*
Рекомендации по теме
welcome to shbcf.ru