Struct vs Classes

Jitendra Deore
4 min readDec 12, 2018

--

Struct and Classes are there from beginning. To understand this lets walkthrough with detail.

Structures and classes are general-purpose, flexible constructs that become the building blocks of your program’s code. You define properties and methods to add functionality to your structures and classes using the same syntax you use to define constants, variables, and functions.

First steps towards it to understand the below concepts.

Difference between Value Types and Reference Types:-

Reference Type

Reference type keeps single copy of its data.

One changes reflect across.

Example — Class

Value Type

Value type keeps a unique copy of its data

One changes reflect to single place.

Example — Struct , Enum , Tuple are value types.

Lets see the below example for more understanding..

Class

class CarClass{var name: Stringvar number: Intinit(name: String, number: Int) {self.name = nameself.number = number}}

Now lets run and check

let carClass = CarClass(name: “Alto”, number: 435)var carClass2 = carClasscarClass2.name = “Swift”carClass2.number = 5423print(carClass.name) // Swiftprint(carClass.number) // 5423print(carClass2.name) // Swiftprint(carClass2.number) // 5423

Why this got replaced this is because of class is reference type

Struct

struct Car{var name: Stringvar number: Intinit(name: String , number: Int) {self.name = nameself.number = number}}

Now lets run and check

var car = Car(name: “City”, number: 123)var car1 = car // Assigning car object to another objectcar1.name = “Civic”car1.number = 12345print(car.name) // Cityprint(car.number) // 123print(car1.name) // Civicprint(car1.number) // 12345

Why this is not replaced i.e Value type…

Please see the below image for more details.

Advantages of Struct over class….

  1. automatically threadsafe due to not being shareable
  2. uses less memory due to no isa and refcount (and in fact is stack allocated generally)
  3. methods are always statically dispatched, so can be inlined.

When to choose value type over reference type.

// Source:- https://developer.apple.com/swift/blog/?id=10

So if you want to build a new type, how do you decide which kind to make? When you’re working with Cocoa, many APIs expect subclasses of NSObject, so you have to use a class.

For the other cases, here are some guidelines:

Use a value type when:

  1. Comparing instance data with == makes sense
  2. You want copies to have independent state
  3. The data will be used in code across multiple threads

Use reference type when:

  1. Comparing instance identity with === makes sense
  2. You want to create shared, mutable state

Common things in Struct and Classes:-

  1. Define properties to store values
  2. Define methods to provide functionality
  3. Define subscripts to provide access to their values using subscript syntax
  4. Define initializers to set up their initial state
  5. Be extended to expand their functionality beyond a default implementation
  6. Conform to protocols to provide standard functionality of a certain kind

Classes have additional capabilities that structures don’t have:

  1. Inheritance enables one class to inherit the characteristics of another.
  2. Type casting enables you to check and interpret the type of a class instance at runtime.
  3. Deinitializers enable an instance of a class to free up any resources it has assigned.
  4. Reference counting allows more than one reference to a class instance.

Which are value type in Swift and which all are reference type.

Value Type:-

1 Array

2 String

3 Dictionary

4 Set

5 Struct

6 Enum

7 Tuple

Reference Type :-

1 Functions

2 Closures

3 Class

Many people says struct is faster than class lets see how it is faster

Structs are value type and classes are reference type

One reason that value types are more performant than reference types is because value types are allocated statically, whereas reference types are allocated dynamically. What does this mean and why is static allocation a performance boost?

Recall that when something is allocated statically, its size is fixed when the program is created. Primitive types like int, boolean, char, are allocated statically, i.e. on the stack.

Primitive types like int, boolean, char, are allocated statically, i.e. on the stack.

That’s it for now.

If you have any questions on this please do comment and follow me.

This story is published in Noteworthy, where 10,000+ readers come every day to learn about the people & ideas shaping the products we love.

Follow our publication to see more product & design stories featured by the Journal team.

--

--

Jitendra Deore

Mobile Head at Powerplay. Would love to explore the new tech things and would love to visit new places.