Structure and Class in Swift

Nowadays Swift pamper Structure instead of Class.

After Protocol Oriented Programming in Swift, Swift provides a number of features that make Structure better than classes in many circumstances.

Do you want to know why?

In Objective-C, NSString is a class whereas in Swift String is Structure.

When we say swift is faster than objective-c. There are many reasons to make it fast and using Structure is one of the reasons.

  1. Performance Optimizations

Although strings in Swift have value semantics, strings use a copy-on-write strategy to store their data in a buffer. This buffer can then be shared by different copies of a string. A string’s data is only copied lazily, upon mutation, when more than one string instance is using the same buffer. Therefore, the first in any sequence of mutating operations may cost O(*n*) time and space.

2. Another reason is storage location:

Structure properties are stored on Stack and Class instances are stored on Heap hence, Structure is faster than a class.

*****************************************************************

Why stack is faster than heap?

1. Accessing-time of heap takes is more than a stack.

2. Handling of the Heap frame is costlier than handling of the stack frame.

3. Stack frame access is easier than the heap frame as the stack has a small region of memory and is cache-friendly but in case of heap frames which are dispersed throughout the memory so it causes more cache misses.

4. The stack is faster than the heap because stack memory is guaranteed to be released in the reverse order it is allocated. This makes it much easier to manage (no need to merge free areas for example) and optimizes the locality of memory accesses.

******************************************************************

Structures are preferable if they are relatively small and copyable because copying is way safer than having multiple references to the same instance as happens with classes.

While using Structure, you don’t need to worried about any kind of modification on your variable in a multithreaded environment. As everyone has own copy of the object.

Sample code to describe value and reference type :

Difference between Structure and class

  1. Classes have Inheritance which allows one class to inherit.

2. Structures are value types it copies the same memory into another location, and classes are reference type used the same memory.

3 Structure properties are stored on Stack and Class instances are stored on Heap hence, Structure is faster than a class.

4. The structure gets a default initializer automatically whereas, in Class, we have to initialize.

5. Classes can be deinitialized, i.e. you can call a function before the class is destroyed

5. Structure is thread-safe by default. In Case of Class, we need to do some extra work to make it thread-safe.

What classes and structs have in common:

  • an extension and protocol is available for both
  • They can define properties and functions
  • subscripts can be used by both
  • both can use init()

If you find this article is helpful please do not forget to share, recommend, and clap.

Postgraduate from JEC Jabalpur || iOS and macOS developer, traveller |||| Team Lead at Optimize IT Systems