Class VS Struct

Nitish Kumar
3 min readJun 19, 2023

Hello everyone, In this post we will learn difference between Class VS Struct using two marvel famous character.

In the Marvel Cinematic Universe, Iron Man and Captain America are two iconic superheroes with distinct abilities and approaches. Similarly, in Swift programming, structs and classes possess unique characteristics that influence how data is organized and manipulated. In this article, we’ll explore the differences between structs and classes by drawing parallels to these Marvel characters, helping you understand when to unleash the powers of Iron Man (struct) or Captain America (class) in your iOS app development journey.

Captain America (Class) VS Iron Man (Struct)
  1. Origin Story:
    Iron Man (Struct):
    - Structs are value types, created and destroyed on the stack, just like Iron Man suiting up and removing his armor.
    - Each instance of a struct is independent, with its own copy of data, resembling Iron Man’s individuality and self-contained abilities.
struct IronMan {
var suitName: String
var abilities: [String]
}
var mark1 = IronMan(suitName: "Mark I", abilities: ["Flight", "Repulsor Beams"])
var mark2 = mark1 // Copying the struct instance
mark1.suitName = "Mark XLVII" // Updating the value of mark1
print(mark1.suitName) // Output: Mark XLVII
print(mark2.suitName) // Output: Mark I (mark2 is a separate copy)

Captain America (Class):
- Classes are reference types, residing in memory heap, similar to Captain America leading his team, the Avengers.
- Multiple instances of a class can point to the same reference, allowing shared access to data, like how Captain America unites others under a common mission.

class CaptainAmerica {
var shieldColor: String
var abilities: [String]

init(shieldColor: String, abilities: [String]) {
self.shieldColor = shieldColor
self.abilities = abilities
}
}
let steveRogers = CaptainAmerica(shieldColor: "Red, White, and Blue", abilities: ["Enhanced Strength", "Shield Combat"])
let samWilson = steveRogers // Both samWilson and steveRogers refer to the same instance
steveRogers.shieldColor = "Blue" // Modifying the value of steveRogers
print(steveRogers.shieldColor) // Output: Blue
print(samWilson.shieldColor) // Output: Blue (samWilson and steveRogers refer to the same instance)

2. Superpowers:
Iron Man (Struct):
- Structs possess value semantics, making them suitable for representing simple data structures and immutable values.
- Like Iron Man’s advanced technology, structs offer powerful capabilities like encapsulating properties, methods, and initializers.

Captain America (Class):
- Classes exhibit reference semantics, enabling advanced features and dynamic behaviors.
- Classes can inherit properties and methods from a superclass, forming hierarchies like Captain America’s leadership within the Avengers.

3. Immutability:
Iron Man (Struct):
- Structs promote immutability by default, ensuring instances cannot be modified after creation.
- This aligns with Iron Man’s unwavering armor design, where the structure remains unchanged once created, reinforcing stability and predictability.

Captain America (Class):
- Classes allow for mutable properties, empowering dynamic updates to an instance’s state.
- Similar to Captain America’s adaptable shield, classes can modify their properties during runtime, facilitating flexibility and responsiveness.

4. Copying and Identity:
Iron Man (Struct):
- Structs are copied when assigned or passed as arguments, ensuring changes are isolated to the current instance.
- This copying behavior aligns with Iron Man’s armor, where each suit represents a unique copy with its own characteristics.

Captain America (Class):
- Classes are passed by reference, allowing multiple instances to refer to the same reference.
- Like Captain America’s shield, which remains the same regardless of who wields it, class instances maintain their identity even when referenced from different locations.

5. Performance Considerations:
Iron Man (Struct):
- Structs are lightweight, stack-allocated, and have better performance characteristics.
- This efficiency is akin to Iron Man’s agility, enabling swift execution and reduced memory overhead.

Captain America (Class):
- Classes involve additional memory management overhead due to reference counting.
- Similar to Captain America’s steadfastness, classes offer more flexibility and power, allowing for complex relationships and shared state.

Conclusion:
Just as Iron Man and Captain America complement each other within the Marvel Universe, structs and classes play vital roles in Swift development. Structs excel in scenarios where immutability, value semantics, and lightweight performance are crucial. On the other hand, classes shine in managing shared state, dynamic behavior, and complex hierarchies.

Hope you enjoyed reading this post!!

If you have any comment, question, or recommendation, feel free to post them in the comment section below!

Thank you!!! Happy coding.

--

--

Nitish Kumar

I developed and maintain applications aimed at a range of iOS devices including mobile phones and tablet computers.