Class vs Struct in Swift

  • Property
  • Method/functions
  • Initializers
  • Protocol
  • Extension
1. Struct and 2. Class

What are common things in struct and class?

  • Both has Property, function, initializer, protocol and extension.
  • Define subscript to provide access to their values.
  • Failable init can be used in both struct and class
  • both can access property/function using dot syntax . e.g: structObjectName.propertyname or classObjectName.propertyname
  • both as default init method i.e .init()
struct and class initializer.

Things Available Only in Class:

  • deinit can be used only with class.
  • Classes can inherit the characteristics of another class. Struct can’t inherit from other struct.
  • Reference counting allows more than one references.
  • Type of class instance can be checked at runtime.

Things Available Only in Struct:

  • Struct has default member wise initializer. For class we need to define explicitly.
1. struct has default member wise initializer and 2. For class you need to explicitly define intializer
  • Structs have the benefit of mutation in safety as you can trust that no other part of your app is changing the data at the same time.
  • Because of mutation in safety benefit, struct is helpful in multi-threading.
mutating keyword used to modify self property in swift.

Difference between struct and class:

  • Class uses keyword class and Struct uses keyword struct .
  • For modifying the self property in struct from a function you need to define mutating at the beginning of function. Class doesn’t required anything.
  • One of the most difference is value vs reference type.
  • If you create struct instance with let property, you can’t modify its properties. If you create a class instance with let property, you can modify its properties. i.e:

Value vs reference types:

  • Classes are reference type : which means reference to a location in the memory where class instance is stored.
  • Struct and enumeration are value type: which means each reference has it’s own copy of data
  • If you share/assign instance of class to different references, all these references will have a single shared data. That means any changes in that class instance will be available to every reference
  • If you share/assign instance of struct to different references, every reference will creates its own memory to store the data. Any changes in struct in one reference will not affect in other reference.
1. output:varsha, deeksha 2. varsha, varsha
1. A,B,C are referring to same class object if we share the data through class. 2. A,B,C are referring to different struct object if we share the data through struct.

Why class and struct are different?

How can you decide which one is better?

  • If you want to compare object by reference using === then go for class and If you want to compare object by reference using == then go fo struct.
  • Use struct when you need mutable copy and use class when you need single copy.

--

--

--

iOS Developer in walmart

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Running Cucumber tests with Jenkins

Think like a product manager

Balance of giving and receiving: Golden ratio

Types in C/C++ and Julia

Mint Overview

Blog Article 02

Web Designing and Development — A Synopsis

Unit Testing for Android in Depth

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Manasa M P

Manasa M P

iOS Developer in walmart

More from Medium

A Swift transition

Balanced Brackets Solution in Swift | Hackerrank

The Power of UICollectionView Compositional Layout | Swift | UIKit

Phantom Types in Swift