langShiftlangShift

Classes and Structures - OOP, Properties, Methods

Learn Swift classes and structures: OOP basics, properties, methods, constructors, and comparison with JavaScript

Classes and Structures: OOP, Properties, Methods

In this module, we explore object-oriented programming (OOP) in Swift and compare it with JavaScript's class and prototype system. Swift provides both classes (reference types) and structures (value types), each with their own use cases and features.

Table of Contents

Introduction: Classes vs Structures

  • Class: Reference type, supports inheritance, deinitializers, identity comparison, reference semantics.
  • Struct: Value type, no inheritance, copied on assignment, value semantics, lightweight and preferred for simple data models.
FeatureJavaScript ClassSwift ClassSwift Struct
Reference TypeYesYesNo
Value TypeNoNoYes
InheritanceYesYesNo
DeinitializerNoYesNo
MutabilityDynamicControlledControlled
ProtocolsNo (interface)YesYes
Use CaseAll OOPOOPData models, lightweight types

Declaring Classes and Structures

Loading editor...

Properties

Swift supports stored properties, computed properties, and property observers. JavaScript properties are always dynamic.

Loading editor...

Methods

Both JavaScript and Swift support instance methods. Swift also supports static/class methods and mutating methods for structs.

Loading editor...

Initializers (Constructors)

Swift uses init for initializers. Structs get a memberwise initializer by default.

Loading editor...

Inheritance and Polymorphism

Swift classes support single inheritance, while JavaScript classes also support single inheritance but can use mixins and composition.

Basic Inheritance

Loading editor...

Polymorphism

Loading editor...

Value Types vs Reference Types

This is a fundamental difference between Swift and JavaScript.

Reference Types (Classes)

Loading editor...

Value Types (Structs)

Loading editor...

Protocols (Interfaces)

Swift protocols are similar to JavaScript interfaces but more powerful.

Loading editor...

Advanced Features

Property Wrappers and Computed Properties

Loading editor...

Static and Class Methods

Loading editor...

Exercises

Exercise 1: Create a Student Management System

Loading editor...

Exercise 2: Implement a Shape Hierarchy

Loading editor...

Key Takeaways

Swift OOP Advantages

  1. Value Types: Structs provide value semantics, preventing unexpected mutations
  2. Type Safety: Strong typing prevents runtime errors
  3. Protocols: Flexible interface system for both classes and structs
  4. Property Observers: Built-in support for property change notifications
  5. Memory Management: ARC handles memory automatically

Key Differences from JavaScript

  1. Value vs Reference: Swift distinguishes between value and reference types
  2. Inheritance: Only classes support inheritance in Swift
  3. Mutability: Swift structs require explicit mutating methods
  4. Protocols: Swift has formal protocol system vs JavaScript duck typing
  5. Memory Management: ARC vs garbage collection

Best Practices

  1. Use structs for simple data models and value semantics
  2. Use classes for complex objects that need inheritance
  3. Leverage protocols for flexible interfaces
  4. Prefer composition over inheritance when possible
  5. Use property observers for reactive programming
  6. Consider value types for thread safety and performance

Next Steps

In the next module, we'll explore protocols and extensions in Swift, including protocol-oriented programming, protocol extensions, and how they compare to JavaScript's approach to interfaces and mixins.